-3

Very early stages of learning Python, my first language. And this little bit of code is coppied straight out of the book. Why do I get an error message?

name = input("Please tell me your name: ")
print("Hello, " + name + "!")

I enter my name, Eric, and get the following error:

File greeter.py, line 1, in ,<module>
    name = input("please tell me your name: ")
file '<string>, line 1, in <module>
NameError: name 'Eric is not defined
ChiDrummer
  • 1
  • 1
  • 1
  • 2
    Check your Python version. If you are using Python 2.x you will need to use `raw_input` instead of `input`. – Hugh Bothwell Jul 18 '17 at 23:15
  • Python 3. but thanks! – ChiDrummer Jul 18 '17 at 23:17
  • 3
    ... make certain, because that is exactly the error message I would expect if you were actually running Python 2. – Hugh Bothwell Jul 18 '17 at 23:18
  • 1
    @ChiDrummer Please just substitute `input` with `raw_input` and see what happens. – cs95 Jul 18 '17 at 23:22
  • Also, it looks like you retyped your error message manually instead of copy-pasting it out of the interpreter output. Please copy-paste instead of retyping code or error messages; retyping often introduces transcription errors that make it harder to debug things. You seem to have made a number of errors while retyping the error message in this question. – user2357112 Jul 18 '17 at 23:22
  • 2
    Possible duplicate of [input() error - NameError: name '...' is not defined](https://stackoverflow.com/questions/21122540/input-error-nameerror-name-is-not-defined) – AGN Gazer Jul 18 '17 at 23:26
  • now i'm having doubts – ChiDrummer Jul 18 '17 at 23:27
  • @ChiDrummer Can you please? – cs95 Jul 18 '17 at 23:28
  • thought it was running python 3. command prompt returns python 3.6.1 but text editor, somehow, is linked to a python folder Python27. When I run the code with raw_input it works. In trying to reconfigure my text editor, I cant find another Python file, ie Python36. Can only find Python27 – ChiDrummer Jul 18 '17 at 23:35
  • on windows python 3.6 defaults to a hidden folder when installing. for me, its in `C:\Users\\AppData\Local\Programs\Python\Python36-32`. Hope that helps you find it – jacoblaw Jul 18 '17 at 23:39
  • Run `py -3 -c "import sys; print(sys.prefix)"` to see where the default Python 3 installation is located. – Eryk Sun Jul 18 '17 at 23:41

2 Answers2

1

Just use the raw_input() function instead. In early versions of python, the input would be run as regular python (through the eval() command). To get around this, use the raw_input() function.

name = raw_input("Please enter your name: ")
print("Hello, " + name + "!")
>>>Please enter your name: Eric
>>>Hello, Eric!
Matthew
  • 472
  • 2
  • 5
  • 12
1

When executing your program through a terminal, try to type 'python3 fileName.py' instead of 'python fileName.py' this may work if you are sure that you use python version 3, otherwise, just use raw_input assuming you are using an old version of python.