tags:

views:

144

answers:

2

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
UserName = input("Please enter your name: ")
File "", line 1, in
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?

+6  A: 

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

Ignacio Vazquez-Abrams
When was Input() used? In previous versions?
Sergio Tapia
Yes. Python version 2.x. http://docs.python.org/library/2to3.html#2to3fixer-input
Alok
`input()` was always broken. 3.x "fixed" it, but the damage will take a long time to heal.
Ignacio Vazquez-Abrams
+2  A: 

@OP, in 2.x, input() "evaluates" what is typed in. (see help(input) ). therefore, when you key in "k", input() try to find what "k" is. because its not defined, it gives error. Use raw_input() in 2.x. In 3.0x, input() is fixed. IF (and not advisable) you really want to use input(), then quote your "k" variable.

eg

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