1

Here is some basic code I've made:

userInput = input("Type in a random word:\n")
print("Here are the letters in that word")
for letters in userInput:
    print(letters)

Completely works in the 3.5 shell/IDLE but wont work in my terminal... Here is the error when i put the input as "test":

NameError: name 'test' is not defined

Any help?

Amir
  • 10,600
  • 9
  • 48
  • 75
K. L.
  • 21
  • 3
  • Are you doing this in the `python` console (i.e., at the `>>>` prompt)? Using a raw input reader (like the `input()` function) in the Python 3 REPL is problematic, because the REPL is already reading raw input. Try putting the same code in a file (e.g., "foo.py") and running it (e.g., `python3 foo.py`). That works fine for me on my machine. – Brian Clapper Jan 03 '16 at 23:51
  • No, I saved it as ForLoopTest.py and it still gives me the error in terminal. Also this error persists in a lot of my practice files when I get user input. – K. L. Jan 03 '16 at 23:55
  • Run `python -V` to see the version of the interpreter you are running in your terminal. – John Keyes Jan 04 '16 at 00:01
  • Oh I see. It says I'm running Python 2.7.5 How do I change that – K. L. Jan 04 '16 at 00:09
  • Check if Python 3 is installed: `which python3`. If it is you run your program `python3 foo.py` as @BrianClapper mentioned above. If it's not available, you'll have to install Python 3. – John Keyes Jan 04 '16 at 00:15

2 Answers2

4

In the terminal you are really running Python 2. raw_input() in Python 2 is the equivalent to input() in Python 3. input() in Python 2 will evaluate the input as a Python statement, so it tries to evaluate test as a variable name.

If you are using Windows you can use the Python Launcher (py.exe) to specify the version of Python to run if you have multiple versions installed. It should already be in the path if you've installed Python 3. In Linux python3 should work.

Example:

C:\>py -3
Python 3.5.0 (v3.5.0:374f501f4567, Sep 13 2015, 02:27:37) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> input()
test
'test'
>>> ^Z

C:\>py -2
Python 2.7.11 (v2.7.11:6d1b6a68f775, Dec  5 2015, 20:32:19) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> input()
test
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 1, in <module>
NameError: name 'test' is not defined
Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251
1

You are running the code using Python 2 the second time. Instead of using the command python, you can use python3 which should work.

In Python 2 you should use raw_input() instead, as the input function includes a call to the eval() function which attempts to execute the input as Python code. raw_input() takes the input as a string. In Python 3 raw_input() has been replaced by input().

G. Grasso
  • 111
  • 4
  • @G.Grasso you went from an answer that provided a good explanation, to one that just mentions the versions of Python and not the reasons why. – John Keyes Jan 04 '16 at 00:06
  • `str(input())` in Python 2 will still try to evaluate the input and give the same error. `raw_input()` is correct for Python 2 and is equivalent to `input()` in Python 3. – Mark Tolonen Jan 04 '16 at 00:06
  • I have fixed it as you described. You were right, my mistake. – G. Grasso Jan 04 '16 at 00:11