1

I'm new to python and have problems with input. When I'm using command userName = input('What is your name? ') it says something like this:

>>> userName = input('What is your name? ')
What is your name? Name
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 1, in <module>
NameError: name 'Name' is not defined

What I must do with it?

Vincent Savard
  • 34,979
  • 10
  • 68
  • 73
Andrew Zitsew
  • 45
  • 1
  • 6
  • 2
    Which version of Python are you using? If you're using Python 2, you should call `raw_input` instead of `input`. – Sam Estep Feb 12 '16 at 17:53
  • 3
    Possible duplicate of [Differences between \`input\` and \`raw\_input\`](http://stackoverflow.com/questions/3800846/differences-between-input-and-raw-input) – Andy Feb 12 '16 at 17:54
  • 1
    You seem to be using Python 2. Your code would be valid Python 3, but you indeed have to change `input` to `raw_input` in your case, as per @Andy comment – Salomé Feb 12 '16 at 17:56

2 Answers2

2

change it to :

userName = raw_input('What is your name?')

In Python 2.x:

raw_input() returns string values and
input() attempts to evaluate the input as command

But in python 3.x, input has been scrapped and the function previously known as raw_input is now input.

Jumayel
  • 96
  • 4
1

The function input() evaluates input as a command--that is why numbers work, but not generic strings that cannot be executed. To achieve attaching a string to a variable, use the more universal raw_input(), which reads everything as a string.

Your new code will now look like

userName = raw_input('What is your name? ')

Best of luck, and happy coding!

Joseph Farah
  • 2,463
  • 2
  • 25
  • 36