Last updated: Feb 2, 2023
Reading time·2 min
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: ')
.
Here is an example of how the error occurs.
# ⛔️ NameError: name 'raw_input' is not defined s = raw_input('Your name: ') print(s)
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.
# ✅ Using input() instead of raw_input() s = input('Your name: ') print(s)
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 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.
num1 = input('Enter num 1: ') print(num1) # 👉️ '10' num2 = input('Enter num 2: ') print(num2) # 👉️ '10' result = int(num1) * int(num2) print(result) # 👉️ 100
We converted the strings to integers before multiplying them.
You can convert the values to floating-point numbers if taking floats from user input.
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 to providing the prompt
argument is to use calls to the
print() function.
print('Your name: ') s = input() print(s)
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.