What is this NameError and how do I fix it?

Question:

I am having difficulty understanding the concept in my book. I tried the coding example the book told me to try, but I get this NameError that I do not understand why it is an error.

This is my code:

name = input('What is your name?')
print(name)

This is my error message:

File "/Users/eric/Python Notes/Notes.py", line 1, in <module>
    name = input('What is your name?n')
  File "<string>", line 1, in <module>
NameError: name 'eric' is not defined

Edit: Thank you everyone who helped me understand this error. By default, Geany and VSCode were using Python2 as my interpreter. Changing it to Python3 fixed the problem. The raw_input() is correct also, because it is the correct syntax for Python2.

Asked By: LilPotato

||

Answers:

there are some solution for your problem:

  1. if you are using python 2.x you should use raw_input instead of input
  2. check your python version by typing python --version or excute your python file in terminal with python3 yourfilename.py
    instead of python yourfilename.py
Answered By: Ali
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.