How to Fix NameError in Python for Beginners

If you are learning Python and practicing coding, you must have certainly faced a NameError. This is a very common error in Python that often troubles beginners.

When you misspell the name of a variable or function in Python, or try to use one without defining it, a “NameError: name is not defined” error occurs. This is a common issue, but it can be very confusing if you are not aware of it.

In this guide, we will understand what is NameError in Python, why it occurs, and how to fix it. We will also look at examples and solutions.

What is NameError in Python?

In Python, a NameError occurs when you try to access a variable or function that has not yet been defined, is misspelled, or is in the wrong scope.

A NameError occurs when running code in your Python IDE (PyCharm, VS Code, etc.), in the terminal or command prompt, or in a Jupyter Notebook.

When a NameError occurs, you see this message on the screen:

NameError: name 'variable_name' is not defined

Or

NameError: name 'function_name' is not defined

Common Examples of NameError

1. Using Variable without defining it

print(name)  # NameError! 'name' is not defined

2. Wrong spelling

age = 25
print(Age)   # NameError! 'Age' is not defined

3. Calling function without defining it

greet()      # NameError! 'greet' function is not defined

How to Fix NameError in Python 

Now, let’s see how to fix the NameError.

1. Define the variable first, then use it

Step 1: Define your variable.

Name = “Errorease” # variable is defined

Step2: Now use the variable.

print(name) # Output: Errorease

Example:

# Wrong
print(city)  # NameError!

# Right
city = "Jaipur"  # First define
print(city)      # Then use- Output: Jaipur

2. Check the spelling of the variable name

Often, a ‘Name Error’ occurs simply because of a spelling mistake! 

Step 1: Check the exact variable name.

student_name = "Anil"  
print(Student_name)    # Wrong - Capital S 

Step 2: Use the exact same spelling.

student_name = "Anil"  # Define 
print(student_name)    # Right - Output: Anil

3. Define the function first, call it later

Step 1: Define the function

def greet():
    print("Namaste!")

Step 2: Then call the function.

greet()  # Output: Namaste!

Example:

# Wrong
wish()   # NameError! Function is not defined

# Right
def wish():          # First define function
    print("Hello!")

wish()               # Then call it - Output: Hello!

4. Check Import Statement

A NameError can occur even when using an external library.

Step 1: First, import the module.

import math

Step 2: Then use  it.

result = math.sqrt(16)  #  Output: 4.0

Common Import Mistakes:

# Wrong
print(sqrt(16))  # NameError!The  sqrt was not imported

# Right
import math
print(math.sqrt(16))  # Output: 4.0

# Or
from math import sqrt
print(sqrt(16))  # Output: 4.0

5. Think about the scope

Variable scope matters! If you define a variable inside a function and try to access it from outside, you will get an NameError error.

# Wrong
def my_function():
    x = 10  # x is defined only within the function.

print(x)  # NameError! 
# Right
x = 10  # Global variable
def my_function():
    print(x)  

print(x)
my_function()
Prevention Tips 
  • Define Variables Before Using Them – Always define a variable, function, or object before using it. If you use a name that is not defined, a NameError will occur.
  • Check for Spelling and Case Sensitivity – Python is a case-sensitive language; `name`, `Name`, and `NAME` are three distinct variable names. Therefore, pay attention to typos and the use of uppercase and lowercase letters.
  • Understand Variable Scope – Look, if you create a variable inside a function, you cannot use it outside that function. Therefore, understanding variable scope is essential to avoid a `NameError`.
  • Use a Code Editor with Linting and Autocomplete – Editors like VS Code or PyCharm highlight undefined variables and typing errors in advance. This allows the issue to be fixed before a `NameError` occurs.
 FAQs

1. What is NameError in Python?

Ans: NameError is a Python exception that occurs when you use a variable, function, or object that has not been defined or is not available in the current scope.

2. How to Fix NameError?

Ans: To fix NameError in Python, first read the error message carefully. Then, check whether the variable or function has been defined, if the spelling is correct, and if it is available in the current scope.

3. How to fix NameError name is not defined in Python?

Ans: If you face `NameError: name is not defined` in Python, first check whether you have defined the variable or function before using it. Next, verify the spelling and the variable’s scope; if you are using a module, ensure it has been imported correctly.

Conclusion

In Python, a `NameError` is a common error that occurs when you try to use a variable or function that has not been defined, or when a defined variable is not available in the current scope.

To fix this, define the variable, check the spelling, verify the scope, and correctly import any necessary modules. By following these steps, you can easily resolve a `NameError`.

Leave a Reply

Your email address will not be published. Required fields are marked *