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.
Answers:
there are some solution for your problem:
- if you are using python 2.x you should use
raw_input
instead of input
- check your python version by typing
python --version
or excute your python file in terminal with python3 yourfilename.py
instead of python yourfilename.py
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.
there are some solution for your problem:
- if you are using python 2.x you should use
raw_input
instead ofinput
- check your python version by typing
python --version
or excute your python file in terminal withpython3 yourfilename.py
instead ofpython yourfilename.py