. Advertisement .
..3..
. Advertisement .
..4..
Python is a versatile language that is quite popular among programmers because of its simplicity and ability to design web applications and other programs. Beginners are also trying to learn Python and have been trying different projects to become a pro. They often land up in some errors and NameError: name ‘raw_input’ is not defined in Python is one of those.
The NameError: name ‘raw_input’ is not defined occurs when used in Python 3. The 3 version of Python uses a bit different function to perform an action as compared to Python 2. This error is caused because of the use of the Python 3 version, which doesn’t accept the raw_input function.
Let’s have a look at how we can fix NameError: name ‘raw_input’ is not defined in Python
How To Fix NameError: name ‘raw_input’ is not defined in Python
NameError: name ‘raw_input’ is not defined in Python
While working with Python 3, you can’t pass the raw_input()
function to take the input from the user. You need to use the input()
function instead of raw_input. When everything is about user-friendliness, taking input from the user is also a user interactive element, and for this purpose, we use the input()
function.
Let’s have an example of an error
str = raw_input('Your name: ') # NameError: name 'raw_input' is not defined
print(str)
In Python 3, the raw_input
is replaced with the input()
function. After changing the code with input()
, we get
str = input('Your name: ') # using input() instead of raw_input()
print(str)
Here, the input()
function has been taking the ‘prompt’ optional argument and then writing it to output with no trailing newline. Once the function reads the input lines, it converts it to a string, and after that, you get the result in return. The “prompt arguments also work to call the print() function.
print('Your name: ')
str = input()
print(str)
Example Scenario
Let’s say we are creating a program that calculates the scores of students on an art homework test, which has 100 points. Based on the total score, the score can be A, B, C, D or fail. Let’s start by asking the user to enter a score so we can calculate it.
test_score = int(raw_input("Enter test scores: "))
if test_score > 90:
score = "A"
elif test_score > 75:
score = "B"
elif test_score > 60:
score = "C"
elif test_score > 50:
score = "D"
else:
score = "Fail"
print("This student earned a {} score with a score of {}.".format(score, test_score))
Error message
Traceback (most recent call last):
File "code.py", line 1, in <module>
test_score = int(raw_input("Enter test scores: "))
NameError: name 'raw_input' is not defined
Solution
We need to replace it with input()
test_score = int(input("Enter test scores: "))
if test_score > 90:
score = "A"
elif test_score > 75:
score = "B"
elif test_score > 60:
score = "C"
elif test_score > 50:
score = "D"
else:
score = "Fail"
print("This student earned a {} score with a score of {}.".format(score, test_score))
Output
Enter test scores: 88
This student earned a B score with a score of 88.
Conclusion
And here we are, discussed NameError: name ‘raw_input’ is not defined in Python and how to fix it. The issue is only about the version of Python. Python 3 has been accepting bit different functions as compared to Python 2.
I hope this helps!
Leave a comment