[Tutor] ** Newbie ** - Dumb input() question ...

Danny Yoo dyoo@hkn.eecs.berkeley.edu
Tue, 29 Jan 2002 12:59:15 -0800 (PST)


On Tue, 29 Jan 2002, Chris McCormick wrote:

> Ok, I know I have done this before, but I can't get input() to work.  Here's the transcript of a PythonWin interactive session:
> 
> 
> 
> >>> response = input('Write something here.')
> 
> *** A dialog box comes up, and I enter the text 'hello' **

There's a distinct differenct between input() and raw_input(): input()
will try to actually evaluate whatever you type in.  Here's an example in
the interpreter that demonstrates this:

###
>>> x = 42
>>> y = input("Enter something: ")
Enter something: x
>>> y
42
###

input() actually tries to do something with what we type in!  Usually,
this isn't what we want --- we'd rather just get what the user literally
typed.  For that, we can use raw_input():


###
>>> z = raw_input("Enter something: ")
Enter something: y
>>> z
'y'
###




> Traceback (most recent call last):
>   File "<interactive input>", line 1, in ?
>   File "C:\Python21\Pythonwin\pywin\framework\app.py", line 362, in Win32Input
>     return eval(raw_input(prompt))
>   File "<string>", line 0, in ?
> NameError: name 'hello' is not defined
>
> What's up?  I get this when running modules, too.  It seems to be
> trying to use the collected input as the name of a variable?


Yes, exactly.  The error message is a consequence of input() trying to
figure out what the value of 'hello' is.


Hope this helps!  If you have more questions, please feel free to ask.