3

I'm running python 2.7.10 on Mac OSX 10.9.5m and it's not working. Here's the code:

# YourName.py
name = input("What is your name?\n")
print("Hi, ", name)

Here's the error:

Python 2.7.10 (v2.7.10:15c95b7d81dc, May 23 2015, 09:33:12) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>> 
What is your name?
Ella
Traceback (most recent call last):
  File "/Users/CentCom/Downloads/code/ch01/YourName.py", line 2, in <module>
    name = input("What is your name?\n")
  File "<string>", line 1, in <module>
NameError: name 'Ella' is not defined
>>> 
7
  • It looks like you're trying to use Python 3 syntax on Python 2. You may want to either get a Python 3 interpreter, or use Python 2 learning materials. Commented Nov 5, 2015 at 22:45
  • @user2357112 those are valid syntax in python 2.7... Commented Nov 5, 2015 at 22:48
  • @DavidZemens: Sure, if you want to print a tuple. I doubt the intent was to print a tuple. Commented Nov 5, 2015 at 22:52
  • @user2357112 I'm not speculating about OP's intent. The syntax She's given is valid in python 2.7 , whether it's what you would do in a particular circumstance is not relevant. Commented Nov 5, 2015 at 22:54
  • @DavidZemens: How is it irrelevant? The goal isn't just to make the program stop producing exceptions; the goal is to make the program correct. If the print syntax isn't changed, it is exceedingly unlikely that the program will be correct. Sure, it's a very slight speculation, but it's about on the same level of speculating that the error in the question is the NameError, rather than the interpreter startup message. Warning the questioner about the issue is much more useful than not warning about it. Commented Nov 5, 2015 at 23:02

4 Answers 4

5

use raw_input in python 2.7.1:

name = raw_input("What is your name?\n")

Otherwise you have to rely on the user to know well-enough to input in quoted string. like "David", or the input attempts to evaluate a name (variable/object/etc) and if there is no such name in scope, you'll get the error.

Alternatively, use exception handling:

name = input("What is your name?\n")
try:
    print("Hi, ", name)
except NameError, e:
    print("Please enclose your input with quotes")
Sign up to request clarification or add additional context in comments.

2 Comments

Python 3 removed the input function then renamed raw_input to input.
@ppperry OP is not using Python 3
3

For the version of Python you are using, you should be using raw_input instead of input.

You can change this line of code:

name = input("What is your name?\n")

to this:

name = raw_input("What is your name?\n")

While you are using Python 2 it would be a good idea to only use raw_input. When you use input, Python will always try to "evaluate" the expression you are entering.

Here is an explanation on why using input would not be a good for Python 2:

So if you enter a 5, it will come back as the number 5 (int in Python).

But if you enter bob, it thinks you are giving Python a "variable" called bob to evaluate, but bob is not defined as a variable in your program. This example actually gives the error you are getting:

NameError: name 'bob' is not defined

In Python if you enter a variable that does not exist, that is the error you get. Look at this example I made:

I tried printing the variable d without assigning d anything:

>>> d
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'd' is not defined

So, if you want to give bob as a string to your input, input is expecting you to give bob quotes to make it a valid string like this: "bob". To avoid all this, raw_input is the right way to go.

If you ever decide to use Python 3, Python 3 replaces raw_input with input. But it acts exactly like Python 2's raw_input.

Good luck with your programming!


Here is the documentation on raw_input:

https://docs.python.org/2/library/functions.html#raw_input

Comments

1

Input tries to evaluate if the given string as a program. For a string alone use raw_input. Or you have to quote the string you had on input to allow python to interpret it as a string. For example:

"Ella"

Comments

0

Python 3.X Syntax for getting User Input

name = input("What is your name?\n")

Python 2.X Syntax for getting User Input

name = raw_input("What is your name?\n")

Comments