2

I'm new to Python and have been trying to make a small test game but when I run my program in CMD it gives of this error.

What Is Your Name?
Test
Traceback (most recent call last):
  File "C:\Users\User\Documents\Python Games\Test.py", line 2, in <module>
    myName = input()
  File "<string>", line 1, in <module>
NameError: name 'Test' is not defined

Though this works fine in IDLE. This is the top of the code.

print('What Is Your Name?')
myName = input()
print('Hello '+str(myName)+' It is good to meet you!') 
Scott Weldon
  • 9,673
  • 6
  • 48
  • 67
0Bennyman
  • 23
  • 3
  • It sounds like you're using IDLE from python3, but the script is using python2. You need to use `raw_input()` in python2. – Barmar Apr 29 '16 at 20:52
  • I'm using Python 3, when I put raw_input() i get told it isn't defined. – 0Bennyman Apr 29 '16 at 20:55
  • That's correct. In Python 3, `input()` does what `raw_input()` did in Python 2. You need to make sure that the script is run using Python 3. – Barmar Apr 29 '16 at 20:57
  • 1
    Pick one version of python and use it for everything, to avoid getting confused like this. – Barmar Apr 29 '16 at 20:57
  • How do I uninstall Python 2? I only have Python27 – 0Bennyman Apr 29 '16 at 20:59
  • That's Python 2.7, so it **is** Python 2. – Barmar Apr 29 '16 at 21:00
  • I did install Python 3 so should I delete it and then reinstall 3? – 0Bennyman Apr 29 '16 at 21:02
  • I don't know anything about installing software on Windows, sorry. – Barmar Apr 29 '16 at 21:03
  • Python2 and Python3 co-exist fine on Windows as long as you keep track of which you are using at any time. Current versions of IDLE put the Python version in the title bar to help you keep track. If you type 'python' in Command Prompt console, you get whichever version was last made the 'default'. Read the Windows part of he installing and using guide. – Terry Jan Reedy Apr 29 '16 at 22:58
  • Having only 1 version (for most beginners this should be the most recent 3.x) is, however, easier. You should not need to touch Python 3 if you delete Python 2. – Terry Jan Reedy Apr 29 '16 at 22:59

2 Answers2

2

Its because of python version change. You probably have two pythons installed. Python2.x is in the PATH. And you have the Python 3.x IDLE.

NameError: name 'Test' is not defined is because its python 2.x

Change myName = input() to myName = raw_input().

And it would work in python 2.x

Your IDLE version must be python 3.x where there is no raw_input().

Ani Menon
  • 27,209
  • 16
  • 105
  • 126
0

Explanation

There as been few changes between Python 2.x and Python 3.x .

One of them is the change of the input function.

In Python 2, the input function evaluate what the user is typing. This function is used when the user have to type a number. It will be parsed and the variable containing the return value of input will be of type numeric (float or int).

https://docs.python.org/2/library/functions.html#input

In your case, since the name of the user is probably not a number, and if you are working with Python 2, you should use the function raw_input which return the input of the user as a String without parsing. Meaning that if the user type a number, it is the String containing this number which will be returned.

https://docs.python.org/2/library/functions.html#raw_input

For security reasons, the input function of Python 2 is not recommended to use because the function evaluate the input, and though it is very convenient for parsing numbers without cast, it allows also the user to inject malicious code into your application ;)

>>> print str(input("Enter your name: "))
Enter your name: __import__('os').listdir('.')
['super_secret.txt']

Thus, the input function has been replaced by the raw_input function in Python 3 and the raw_input function has been removed.

Fix

I think that the default behaviour of your os is to call python2 when you want to run python. So, if you want to run python3, you have to say it explicitly:

Your IDE seems to be correctly configured to use python3 as default interpreter.

Luc Giffon
  • 173
  • 14