Imperio Shanks
Imperio Shanks

Reputation: 1

python 3 NameError

Edit: Thank you for your helpful answers! I downloaded Python 3.7.0 but you are right, my Mac is running Python 2.7. I have homework now :) Figure out how to get it running 3.7. I will come back if I have more questions. Thank you!

Beginner here. I'm getting NameError when executing in Mac with Python Launcher. When testing in Python 3.7.0 Shell it works ok. I've read other answers to NameError questions, but do not understand what I'm doing wrong. Help is appreciated.

Code used

first_name = input ("Hi, what's your first name? ")
print ("Hi," , first_name)

Error received

Traceback (most recent call last):
  File "/Users/imperio/Documents/pythonpractice/Name.py", line 1, in <module>
   first_name = input ("Hi, what's your first name? ")
  File "<string>", line 1, in <module>
NameError: name 'Imperio' is not defined

Upvotes: 0

Views: 3480

Answers (2)

Mark Tolonen
Mark Tolonen

Reputation: 178399

You're running Python 2. In Python 2, input executes the input. raw_input just returns the string entered.

Here's an example:

>>> x = 1
>>> y = 2
>>> z = 3
>>> print input('variable? ')
variable? x                       # Note output is the value of the variable
1
>>> print input('variable? ')
variable? w                          # Variable 'w' doesn't exist
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 1, in <module>
NameError: name 'w' is not defined
>>> print raw_input('variable? ')       # Raw input just returns the input.
variable? x
x

Upvotes: 0

Woohoojin
Woohoojin

Reputation: 724

This is most likely because you are not executing it using Python 3+.

Please check the output of python -V to see which version you are executing your code with.

On mac you may already have both installed, Python 3 is sometimes aliased under python3 file.py

Here's your program converted to valid Python2:

first_name = raw_input ("Hi, what's your first name? ")
print ("Hi, {}".format(first_name))

Upvotes: 2

Related Questions