NameError: name ‘raw_input’ is not defined in Python

In Python2, the “raw_input” function is used to accept the input from the user. The “raw_input()” function has been renamed to the “input()” in Python 3. So, when the “raw_input()” function is called in Python3, the “NameError” arises in a program.

This Python write-up will provide a comprehensive guide on why and solutions for the error “name raw_input is not defined” in Python.

Reason: Using raw_input() Function in Python3

The prominent reason which causes this error is when the user practices the “raw_input” function in Python3 rather than the “input()” function. In Python3, the old “raw_input()” function has been renamed to the “input()” function.

The above output shows “NameError” when the “raw_input()” function is used in Python3.

Solution: Use the input() Function

To solve this error, use the “input()” function to accept the input value from the user. An example of this solution using the “input()” function is as follows:

Code:

Value = input('What is Your Name: ')

print(Value)

In the above code, the “input()” function is utilized to input the value from the user.

Output:

The above output shows the value taken from the user and displayed on the screen using the “print()” function.

Example: Taking Input From User to Perform Mathematical Computation

The prompt of the “input()” function always returns the value in string form. To perform any mathematical calculation after taking a value from the user, you need to convert it into an integer or float using the “int()” and “float()” functions.

Code:

x = input('Enter First Number: ')
y = input('Enter Second Number: ')

output = int(x)*int(y)
print('Multiplication of Input Number: ', output)

In the above code, the input() function takes the numeric value from the users and is stored in a variable “x” and “y”. To perform mathematical computation, the “numeric string” is converted into an “int” by utilizing the “int()” function. The “print()” accepts the output variable as an argument and displays the multiplication result on the console screen.

Output:

The above output shows that the multiplication of two input numbers has been calculated successfully.

Conclusion

The “name raw_input is not defined” error arises when the user uses the “raw_input()” function in Python3. To solve this, use the simple “input()” function that takes the “prompt” value as an argument and returns the string. This article presented a detailed guide to the reason and solutions of “NameError: name raw_input is not defined” in Python. Additionally, an example is also provided, which shows the working of the “input()” function.