0

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
3
  • That's not Python 3.7. You're on Python 2. Commented Jul 10, 2018 at 1:06
  • Thank you. I downloaded Python 3.7. from python.org. I'm using a beginners Python book that's based on Python 3.1 Commented Jul 10, 2018 at 1:13
  • Possible duplicate of input() error - NameError: name '...' is not defined Commented Oct 25, 2019 at 0:15

2 Answers 2

2

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))
Sign up to request clarification or add additional context in comments.

Comments

0

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

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.