473,404 Members | 2,187 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

error with string (beginner)

Hello. I get the following error with the following code. Is there
something wrong with my Python installation?

code:
import types
something = input("Enter something and I will tell you the type: ")

if type(something) is types.IntType:
print "you entered an integer"
elif type(something) is types.StringType:
print "you entered a string"

error:
String: Source for exec/eval is unavailable

Jun 25 '06 #1
5 1628
Alex Pavluck wrote:
String: Source for exec/eval is unavailable


I'm not able to find this message anywhere in the Python-2.4.3 sources.
What python version are you using? What input did you enter when the
prompt appeared? and copy&paste the *exact* exception you got including
the full traceback.

Regards
Sreeram
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2.2 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFEnpwXrgn0plK5qqURAk7fAJ9I449qtRT8HbpHFScM6R cbTGL8HwCeP9+G
YM8xbP7i0cyS1Dya56nAv4c=
=X7Ek
-----END PGP SIGNATURE-----

Jun 25 '06 #2

Alex Pavluck wrote:
Hello. I get the following error with the following code. Is there
something wrong with my Python installation?

code:
import types
something = input("Enter something and I will tell you the type: ")

if type(something) is types.IntType:
print "you entered an integer"
elif type(something) is types.StringType:
print "you entered a string"

error:
String: Source for exec/eval is unavailable From the docs:

"""
input( [prompt])

Equivalent to eval(raw_input(prompt)). Warning: This function is not
safe from user errors! It expects a valid Python expression as input;
if the input is not syntactically valid, a SyntaxError will be raised.
Other exceptions may be raised if there is an error during evaluation.
(On the other hand, sometimes this is exactly what you need when
writing a quick script for expert use.)
If the readline module was loaded, then input() will use it to provide
elaborate line editing and history features.

Consider using the raw_input() function for general input from users.
""""

So you want be using raw_input() for starters...

Cheers,

Jon.

Jun 25 '06 #3
I believe what you are trying to do is something like the following.

Expand|Select|Wrap|Line Numbers
  1. def isIntLike(x):
  2. try:    int(x)
  3. except: return False
  4. else:   return True
  5.  
  6. something = raw_input("Enter something and I will tell you the type: ")
  7.  
  8. if isIntLike(something):    print "I am an int"
  9. elif isinstance(something, type('')):    print "I am a string"
  10. else:    print "I am an impostor!"
  11.  
Note that you should be using raw_input() rather than input(). The
former always returns a string. The latter tries to execute the input,
hence your error message.

Also note that everything input on the command line is a string,
regardless of what you type. If you want type checking, use C++ or
java. If you want to check for anything more than IntLike strings, you
should consider using regular expressions through the 're' package.
For user-defined types, use 'isinstance(something, class object)' to
check for a type.

Lastly, you may be interested in the 'getopt' module, which handles
command line arguments (as opposed to prompting user for input)
automatically. See here:
http://python.active-venture.com/lib/module-getopt.html

Check out the Python Cookbook for lots of good examples:
http://aspn.activestate.com/ASPN/Cookbook/Python

HTH,

Jay

Alex Pavluck wrote:
Hello. I get the following error with the following code. Is there
something wrong with my Python installation?

code:
import types
something = input("Enter something and I will tell you the type: ")

if type(something) is types.IntType:
print "you entered an integer"
elif type(something) is types.StringType:
print "you entered a string"

error:
String: Source for exec/eval is unavailable


Jun 25 '06 #4
In article <11**********************@i40g2000cwc.googlegroups .com>,
Alex Pavluck <ap******@gmail.com> wrote:
Hello. I get the following error with the following code. Is there
something wrong with my Python installation?

code:
import types
something = input("Enter something and I will tell you the type: ")

if type(something) is types.IntType:
print "you entered an integer"
elif type(something) is types.StringType:
print "you entered a string"

error:
String: Source for exec/eval is unavailable


There are several things wrong--'least three distinct ones, by my count.

What version of Python do you believe you're using? What input do you
enter at the "Enter something ..." prompt?

I suspect you're going to have happier results all around when you
replace "input" with "raw_input". I encourage you to read the documenta-
tion for each.
Jun 25 '06 #5
Jason wrote:
I believe what you are trying to do is something like the following.

Expand|Select|Wrap|Line Numbers
  1.  def isIntLike(x):
  2.      try:    int(x)
  3.      except: return False
  •  
  • *Never* ever use a bare except clause. *Always* specify wich exceptions
  • you are expecting. (NB : here, TypeError and ValueError).
  •  
  • (NB :  of course, like for all and any do's-and-don't rules, there are
  • actually a very few cases where using a bare except may be ok.)
  •      else:   return True
  •  something = raw_input("Enter something and I will tell you the type: ")
  •  if isIntLike(something):    print "I am an int"
  •  
  • Nope. At this stage, 'something' is *not* an int - it's a string that
  • can be turned into an int.
  •  elif isinstance(something, type('')):    print "I am a string"
  •  
  • You don't need the "type('')" stuff - use isinstance(something,
  • basestring) instead.
  •  else:    print "I am an impostor!"
  •  


  • (snip)

    --
    bruno desthuilliers
    python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
    p in 'o****@xiludom.gro'.split('@')])"
    Jun 26 '06 #6

    This thread has been closed and replies have been disabled. Please start a new discussion.

    Similar topics

    7
    by: Chris Mosser | last post by:
    K... I'm a java beginner and I took a few months off and now I seem to be having a brain fart or something. The following is a simple class I created because I was having trouble running a chat...
    1
    by: AlexB | last post by:
    The ASP.NET account needs access to the following directory: C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322 \Temporary ASP.NET Files Locate the folder in Windows Explorer, right-click on the...
    23
    by: YinTat | last post by:
    Hi, I learned C++ recently and I made a string class. A code example is this: class CString { public: inline CString(const char *rhs) { m_size = strlen(rhs);
    4
    by: 3rdshiftcoder | last post by:
    Hi- I'd like to use this class from a book but am getting a compiler error. On the line private List < String > list; p.932 Chapter 19 found in the code below in the eclipse editor it is...
    7
    by: Yongsub Eric Shin | last post by:
    Hi. I'm just a beginner in ASP.Net. I started writing codes and I keep on getting this Runtime Error page, where it says "Description: An application error occurred on the server. The current...
    9
    by: Prasad | last post by:
    HI, I am a beginner in VC++.. I am trying to write a Win32 console application in visual studio.. I am using following header files.. #include <STRING> using namespace std; #include...
    37
    by: sam44 | last post by:
    Hi, At startup the user log on and chooses the name of a client from a dropdownlist, which then changes dynamically the connection string (the name of the client indicates which database to use)....
    15
    by: Bjorn Jensen | last post by:
    Hi! An beginner question: Pleas help me with this (-: Error (the arrow points on the s in sqrt) ===== tal.java:6: cannot find symbol symbol : method sqrt(int) location: class tal...
    29
    by: aarthi28 | last post by:
    Hi, I have written this code, and at the end, I am trying to write a vector of strings into a text file. However, my program is nor compiling, and it gives me the following error when I try to...
    0
    by: Charles Arthur | last post by:
    How do i turn on java script on a villaon, callus and itel keypad mobile phone
    0
    by: emmanuelkatto | last post by:
    Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
    0
    BarryA
    by: BarryA | last post by:
    What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
    0
    marktang
    by: marktang | last post by:
    ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
    0
    jinu1996
    by: jinu1996 | last post by:
    In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
    0
    by: Hystou | last post by:
    Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
    0
    tracyyun
    by: tracyyun | last post by:
    Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
    0
    agi2029
    by: agi2029 | last post by:
    Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
    0
    isladogs
    by: isladogs | last post by:
    The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

    By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

    To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.