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?

3
  • 2
    Which version of Python are you using? If you're using Python 2, you should call raw_input instead of input. Commented Feb 12, 2016 at 17:53
  • 3
    Possible duplicate of Differences between `input` and `raw_input` Commented Feb 12, 2016 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 Commented Feb 12, 2016 at 17:56

2 Answers 2

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.

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

3 Comments

input doesn't return integer values, it attempts to evaluate the input as Python code.
The docs say Equivalent to eval(raw_input(prompt)), not Equivalent to int(raw_input(prompt)).
@VincentSavard That's right! Thanks for the correction.
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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.