[Tutor] Beginner level: Why doesn't my code work?

Peter Otten __peter__ at web.de
Sun May 19 16:54:28 CEST 2013


Rafael Knuth wrote:

> Thank you, I am using Python 3.3.0

[Oscar]
> In Python 3 you should use input(). In Python 2 you should use
> raw_input(). I'm guessing that you're using Python 2. In Python 2 the
> input() function tries to evaluate whatever the user types in as if it
> was Python code. Since Rafael is not a defined variable it fails. The
> fix is to use raw_input() which just returns a string.

 
[Rafael] 
> I am not sure I understand.
> "Rafael" is the user's in put, and that value is assigned to the variable
> "name".
> I made sure only a string is accepted as input
> 
> name = (str(input("What's your name?"))
> 
> Can you clarify? Thank you in advance.

As Oscar says you are invoking your script with Python 2. Python 2's input() 
function evals user input as a Python expression. For example if you run a 
script

print input("your input please: ")

and you type

1 + 1

the script will print

2

Likewise if you type

Rafael

the script will look up the value of a variable named Rafael. This doesn't 
exist and therefore you get an exception.

But this is all a distraction -- how exactly are you invoking what you think 
is Python 3.3.0? What is your operating system?

If you are using Linux or OSX open a terminal window and try to run your 
script from that terminal window with

python3.3 ~/Documents/3_Tufcik.py




More information about the Tutor mailing list