NameError: name 'raw_input' is not defined in Python [Fixed]

avatar
Borislav Hadzhiev

Last updated: Feb 2, 2023
2 min

banner

# NameError: name 'raw_input' is not defined in Python

The Python "NameError: name 'raw_input' is not defined" occurs when we use the raw_input() function in Python 3.

To solve the error, use the input() function instead of raw_input in Python 3 applications, e.g. s = input('Your name: ').

nameerror name raw input is not defined

Here is an example of how the error occurs.

main.py
# ⛔️ NameError: name 'raw_input' is not defined s = raw_input('Your name: ') print(s)

# The raw_input function has been renamed to input

The raw_input function has been renamed to input in Python 3.

To solve the error replace calls to raw_input with input in your code.

main.py
# ✅ Using input() instead of raw_input() s = input('Your name: ') print(s)

using input function python

Make sure to replace all calls to the raw_input() function with input() in Python 3.

The input() function takes an optional prompt argument and writes it to standard output without a trailing newline.

The function then reads the line from the input, converts it to a string and returns the result.

# The input() function always returns a string

The input function always returns a value of type string, even if the user entered an integer.

If you need to use the values in mathematical operations, use the int() or float() classes to convert the strings to integers or floating-point numbers.

main.py
num1 = input('Enter num 1: ') print(num1) # 👉️ '10' num2 = input('Enter num 2: ') print(num2) # 👉️ '10' result = int(num1) * int(num2) print(result) # 👉️ 100

python input convert to number

We converted the strings to integers before multiplying them.

You can convert the values to floating-point numbers if taking floats from user input.

main.py
num1 = input('Enter num 1: ') print(num1) # 👉️ '5' num2 = input('Enter num 2: ') print(num2) # 👉️ '2.5' result = float(num1) * float(num2) print(result) # 👉️ 12.5

# An alternative of the input() function

An alternative to providing the prompt argument is to use calls to the print() function.

main.py
print('Your name: ') s = input() print(s)

using input function print

This is very similar to passing the prompt argument to the input() function but instead of showing the prompt on the same line, it is displayed on a separate line.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.