Re: Python 3.2 Question

Andrew Henderson <[email protected]> Sat, 12 Jan 2013 10:47:16 -0500 (EST)
Newsgroups gmane.org.user-groups.jaxlug
Message-ID <[email protected]>
On Sat, 12 Jan 2013, Brett Michaels wrote:

> How to get the entire program to execute?
> Hugh

Remove the eval() from what you are doing.  It is used to evalute a string 
as an expression, which you do not need to do.  The way that I have always 
used eval would be like:

>>> year = eval('input("Enter year: ")')

You see, eval() is more like the input to a calculator.  You type in the 
expression that you want evaluated and the answer spits out.  You can even 
compile chunks of Python script and run then using eval().  It is 
useful for debugging, but you don't really need it for what you are doing.

As an example without using eval(), try the following session in Python:

>>> year = input("Enter year: ")
Enter year: 1
>>> print year
1
>>>

You'll see that the variable "year" is being set to 1 without using 
eval().  Next, try typing in your various checks:

>>> if year == 1:
...   print ("Freshman")
...
Freshman
>>> if year == 2:
...   print ("Sophomore")
...
>>>

You'll see that only the "Freshman" case is being triggered, since "year" 
is equal to 1.  If you put the whole chunk of code, without the eval(), in 
a file (say, test.py) and run it as "python test.py", it will run just 
fine.  Or you can enter it line by line in the interactive shell and get 
the same result.  Whichever is easiest for you.

Andrew

---------------------------------------------------------------------
Archive      http://marc.info/?l=jaxlug-list&r=1&w=2
RSS Feed     http://www.mail-archive.com/[email protected]/maillist.xml
Unsubscribe  [email protected]