16

I'm completely lost as to why this isn't working. Should work precisely, right?

UserName = input("Please enter your name: ")
print ("Hello Mr. " + UserName)
raw_input("<Press Enter to quit.>")

I get this exception:

Traceback (most recent call last):  
  File "Test1.py", line 1, in <module>
    UserName = input("Please enter your name: ")
  File "<string>", line 1, in <module>
NameError: name 'k' is not defined  

It says NameError 'k', because I wrote 'k' as the input during my tests. I've read that the print statement used to be without parenthesis but that has been deprecated right?

4
  • 1
    input is equivalent to eval(raw_input(prompt)). You just want raw_input(). Commented Jan 19, 2010 at 2:42
  • 1
    input() is ok with Python 3k. @OP what version of Python are you using? Commented Jan 19, 2010 at 2:50
  • @Sergio This is not related to your question, but you should use lowercase first letter for variable names (such as 'userName' instead of 'UserName'). Commented Jan 19, 2010 at 3:00
  • I'm using the Python that came with Ubuntu. 2.6.4 I think. Can I install Python 3? SHOULD I install Python 3? Commented Jan 19, 2010 at 3:07

3 Answers 3

15

Do not use input() in 2.x. Use raw_input() instead. Always.

Sign up to request clarification or add additional context in comments.

4 Comments

When was Input() used? In previous versions?
input() was always broken. 3.x "fixed" it, but the damage will take a long time to heal.
python 2 is dead, so i can always use 'input' instead of 'raw_input' now?
12

In Python 2.x, input() "evaluates" what is typed in. (see help(input)). Therefore, when you key in k, input() tries to find what k is. Because it is not defined, it raises the NameError exception.

Use raw_input() in Python 2.x. In 3.0x, input() is fixed.

If you really want to use input() (and this is really not advisable), then quote your k variable as follows:

>>> UserName = input("Please enter your name: ")
Please enter your name: "k"
>>> print UserName
k

Comments

0

The accepted answer provides the correct solution and @ghostdog74 gives the reason for the exception. I figured it may be helpful to see, step by step, why this raises a NameError (and not something else, like ValueError):

As per Python 2.7 documentation, input() evaluates what you enter, so essentially your program becomes this:

username = input('...')
# => translates to
username = eval(raw_input('...')) 

Let's assume input is bob, then this becomes:

username = eval('bob') 

Since eval() executes 'bob' as if it were a Python expression, your program becomes this:

username = bob 
=> NameError
print ("Hello Mr. " + username)

You could make it work my entering "bob" (with the quotes), because then the program is valid:

username = "bob" 
print ("Hello Mr. " + username)
=> Hello Mr. bob

You can try it out by going through each step in the Python REPL yourself. Note that the exception is raised on the first line already, not within the print statement.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.