Last modified: Mar 03, 2025 By Alexander Williams

Fix Python NameError: Name 'input' Not Defined

Encountering a NameError in Python can be frustrating, especially for beginners. One common error is NameError: Name 'input' Not Defined. This article explains why this error occurs and how to fix it.

What Causes the NameError: Name 'input' Not Defined?

The NameError: Name 'input' Not Defined occurs when Python cannot find the input function in the current scope. This usually happens due to one of the following reasons:

  • You misspelled the input function.
  • You are using Python 2.x, where input behaves differently.
  • You accidentally overwrote the input function.

Understanding these causes is the first step in resolving the error. Let's explore each scenario in detail.

Misspelling the input Function

One of the most common reasons for this error is a simple typo. For example:


# Incorrect usage
user_input = inpt("Enter something: ")

In this case, Python will raise a NameError because inpt is not defined. The correct function name is input.


# Correct usage
user_input = input("Enter something: ")

Always double-check your code for typos to avoid such errors.

Using Python 2.x Instead of Python 3.x

In Python 2.x, the input function behaves differently. It evaluates the user's input as Python code, which can lead to unexpected behavior or errors. For example:


# Python 2.x
user_input = input("Enter something: ")

If you enter a string without quotes, Python 2.x will raise a NameError. To fix this, use raw_input in Python 2.x or upgrade to Python 3.x.


# Python 2.x fix
user_input = raw_input("Enter something: ")

In Python 3.x, input works as expected, returning the user's input as a string.

Overwriting the input Function

Another common mistake is accidentally overwriting the input function. For example:


# Overwriting input
input = 42
user_input = input("Enter something: ")

Here, input is no longer a function but an integer. Python will raise a NameError when you try to call it as a function. To fix this, avoid using input as a variable name.


# Correct usage
user_input = input("Enter something: ")

How to Fix the NameError: Name 'input' Not Defined

To fix this error, follow these steps:

  1. Check for typos in your code.
  2. Ensure you are using Python 3.x or use raw_input in Python 2.x.
  3. Avoid overwriting the input function.

If you encounter other NameError issues, such as Name 'print' Not Defined or Name 'None' Not Defined, similar debugging steps apply.

Example Code and Output

Here’s an example of correct usage of the input function in Python 3.x:


# Correct usage of input in Python 3.x
user_input = input("Enter your name: ")
print("Hello, " + user_input + "!")

Output:


Enter your name: Alice
Hello, Alice!

Conclusion

The NameError: Name 'input' Not Defined is a common issue in Python, especially for beginners. By understanding the causes and following the steps outlined in this article, you can easily fix this error. Always ensure you are using the correct Python version and avoid overwriting built-in functions.

For more information on handling similar errors, check out our guides on NameError in Functions and Classes and Why Does NameError Occur in Python?.