0

I am starting with Python and have a problem with the .input() method:

The code is simple:

print("Please enter something")
message = input("Test Input: ")
print(message)

As far as I know, .input is supposed to return a string, but when I try to code in the terminal this does not happen.

When I execute the code the following happens: If I just enter a string (without quotation marks) I get the error message

Please enter something
Test Input: Input_test
Traceback (most recent call last):
  File "message.py", line 2, in <module>
    message = input("Test Input: ")
  File "<string>", line 1, in <module>
NameError: name 'Input_test' is not defined

However, when I enter a string with quotation marks, or when I enter a integer, everything works fine:

Please enter something
Test Input: 12
12

Please enter something
Test Input: "Test_input"
Test_input

This behaviour is confusing as I because resources are saying that .input is already returning a string, so why is the code only working when I explicitly enter a string or an integer? Especially the error message (that the input is not being defined) is a mystery to me.

  • 1
    You are not using Python 3. Even though your tag specifies that you are, your error you are receiving based on your usage of `input` indicates you are actually using Python 2. If you are using Python 2, then use `raw_input`. If you actually do intend to use Python 3, then ensure you are actually using the right interpreter. – idjaw Aug 01 '17 at 14:20
  • if you use Python < 3 you can try raw_input() – Ivan Sheihets Aug 01 '17 at 14:22
  • I think you might be using python2 by accident. Input is different between python 2 and 3, try raw_input and see if that works. – Simon Hobbs Aug 01 '17 at 14:22
  • `import sys; print(sys.version)`, see it's Python 2. Use Python 3 or `raw_input`. – Dimitris Fasarakis Hilliard Aug 01 '17 at 14:25
  • Thanks for the answers! I have both versions of python installed and thought the latest one would be used by default. simply starting the script with python3 solved the problem. – user3553471 Aug 01 '17 at 15:51

0 Answers0