Skip to content

How to Resolve Python "NameError: name 'raw_input' is not defined"

Fix the Python 3 error when raw_input() fails by replacing it with input() and handling string-to-number conversions properly for user input.

If you're encountering the NameError: name 'raw_input' is not defined while running Python code, it's a strong indicator that you are using code written for Python 2 in a Python 3 environment. The way user input is handled changed significantly between these major versions.

This guide explains why this error occurs in Python 3 and shows the correct function to use instead.

Understanding the Error: Python 2 vs. Python 3 Input

Python 2 had two primary functions for getting user input:

  • raw_input(): This function read input from the user and always returned it as a string.
  • input(): This function read input and attempted to evaluate it as Python code. This was considered unsafe and confusing.

In Python 3, this was simplified and improved:

  • The old raw_input() function was renamed to input(). The new input() function now always reads input and returns it as a string.
  • The old, unsafe Python 2 input() function was removed.

Therefore, seeing NameError: name 'raw_input' is not defined means Python 3 doesn't recognize the name raw_input because that function no longer exists as a built-in under that name.

The Cause: Using raw_input() in Python 3

The error occurs when you execute code containing the raw_input() function using a Python 3 interpreter.

try:
    user_name = raw_input("Enter your name: ")
    print(f"Hello, {user_name}")
except NameError as e:
    print(e)

The Solution: Replace raw_input() with input()

The direct fix is to replace every instance of raw_input() in your code with the Python 3 equivalent: input().

user_name = input("Enter your name: ")

print(f"Type of input: {type(user_name)}") # Output: <class 'str'>
print(f"Hello, {user_name}")

Output:

Enter your name: Alice
Type of input: <class 'str'>
Hello, Alice

The input() function in Python 3 behaves exactly like raw_input() did in Python 2 – it reads a line from the console and returns it as a string.

Important Note: input() Always Returns a String

A critical point to remember when migrating from Python 2 or learning Python 3 is that input() always returns a string value, regardless of what the user types.

user_age_str = input("Enter your age: ")        # User types: 30

print(f"Value entered: {user_age_str}")         # Output: 30
print(f"Type of value: {type(user_age_str)}")   # Output: <class 'str'>

result = user_age_str + 5 # This would raise a TypeError

Handling Numeric Input with input()

If you expect the user to enter a number (integer or float) and need to perform mathematical operations, you must explicitly convert the string returned by input() to the desired numeric type using int() or float().

Example with Integer Input:

try:
    age_str = input("Enter your age: ")
    
    age_int = int(age_str)
    print(f"Next year you will be: {age_int + 1}")
except ValueError:
    print("Invalid input. Please enter a whole number for age.")

Example with Float Input:

try:
    price_str = input("Enter the price: ")

    price_float = float(price_str)
    tax = price_float * 0.1
    print(f"Price: {price_float:.2f}, Tax: {tax:.2f}")
except ValueError:
    print("Invalid input. Please enter a valid number for price.")

Alternative Prompting Style

The input() function takes an optional argument, which is displayed as a prompt to the user on the same line where they type. If you prefer the prompt to be on a separate line, you can use a standard print() statement before calling input() without an argument.

print("Please enter your city:")
city = input() # No prompt argument needed here

print(f"You entered: {city}")

Conclusion

The NameError: name 'raw_input' is not defined is a clear sign of using Python 2 syntax (raw_input()) in a Python 3 environment.

The solution is straightforward:

  1. Replace all occurrences of raw_input() with input().
  2. Remember that Python 3's input() always returns a string.
  3. Explicitly convert the result to int() or float() if you need to perform numerical operations.

By using the correct input() function and handling type conversions appropriately, you can effectively gather user input in Python 3.

Navigation