Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
My 1st Program
#1
I need help in this simple program! I don't know why I'm getting this error! I'm new to python!

This is what I typed:

print ('WHAT IS YOUR NAME?')
name = input()
print (name)
This was the result:

Output:
WHAT IS YOUR NAME? kevin
Error:
Traceback (most recent call last): File "C:/Python27/pye.py", line 2, in <module> name = input() File "<string>", line 1, in <module> NameError: name 'kevin' is not defined
WHAT IS WRONG HERE?
Reply
#2
Simple answer - this code is intended to run on python3 and you are running it on python2. As you are new to python - install python3 and use it, not python2. Support for python2 will end soon.

The reason behind the problem - in python2 one should use raw_input() instead of input(). In python2 input() evaluates the user input as if it is valid python code, while raw_input() just returns user input as string.
In python3 input() returns user input as string, so it is equivalent to raw_input() in python2.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020