0

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.

LilPotato
  • 13
  • 3
  • https://stackoverflow.com/questions/45178543/error-with-user-input-function-in-python-3 – a a Aug 30 '22 at 03:57
  • 2
    If you're following a course that is making you use Python 2, I would recommend finding another - Python 2 shouldn't be used anymore in 2022, unless it's as part of legacy software where it's inescapable. – Grismar Aug 30 '22 at 04:02
  • It looks like you're on MacOS, so you should be running the python file with `python3 your_file_name.py` (assuming that your book is using Python 3, which it should be in this day and age) – SuperStormer Aug 30 '22 at 04:02
  • "but it gives me the same error" Can you please confirm that you indeed run this code? I.e. that you see `name = raw_input('What is your name?')` in the error message – Dmitry Aug 30 '22 at 04:06
  • @Dmitry You were right. I forgot the compile so it was still running with input() instead of raw_input() – LilPotato Aug 30 '22 at 04:12

1 Answers1

0

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
Ali
  • 376
  • 5
  • 16
  • I am using MacOS and VSCode as my IDE. How do I make it so that I am running on Python3 instead of Python2? – LilPotato Aug 30 '22 at 04:10
  • open a terminal in vscode from Terminal -->new Terminal in opened terminal type `python3 notes.py` – Ali Aug 30 '22 at 04:13