[Solved]: Python NameError: name ‘XYZ’ is not defined


In Python, NameError occurred for identifier when it’s being used but not defined in local or global scope so Python will able to find and throw exception. NameError can be occurred by following reasons:

1: Misspelled built-in function
2: Using unidentified variables
3: Define variable after used
4: Incorrect Usage of Scope

Note : In any programming language Identifier is name of variable, function, class or object.

We will discuss in detail about all these above cases with example.

Example : Misspelled built-in function

In this example by mistake not type correct name for print function. I have written as ‘prin’ instated of ‘print’ that’s why Python with throw NameError.

sugar_weight = input("Enter how much sugar you want: ")
prin(sugar_weight)

Output

   prin(sugar_weight)

NameError: name 'prin' is not defined

Example : Using unidentified variables

In this example calculating the sub of variable ‘A’ and ‘B’ but variable B is not defined that’s why Python with throw NameError.

A=5
sum=A + B
print("Sum:"+sum)

Output

   sum=A + B

NameError: name 'B' is not defined

Example : Define variable after used

In this example the variable sum is getting printed before going declare or assigned that’s why Python with throw NameError.

A=5
B=10
print("Sum:"+add)
add=A + B

Output

 print("Sum:"+add)

NameError: name 'add' is not defined

Example : Incorrect Usage of Scope

In this example the avg variable is having local scope with in the function calculateAverage but trying to access for print outside the function. In this case This avg variable will be undefined out the calculateAverage() method then Python will throw NameException.

def calculateAverage(numbers):
    avge = sum(numbers)/len(numbers)

numbers=[10,20,30,40,50] 
calculateAverage(numbers)
print(avge)

Output

  print(avge)

NameError: name 'avge' is not defined

Solution for NameError

In Python NameError related to variable or function name must be fix by modifying the name of variable or correct the code. In case NameError can occurred on runtime then you can explicitly use try and except block to handle NameError.

def calculateAverage(numbers):
    avge = sum(numbers)/len(numbers)
try:
	numbers=[10,20,30,40,50] 
	calculateAverage(numbers)
	print(avge)
except NameError:
    print ("NameError occured in code because some variable isn't defined.")

Output

NameError occured in code because some variable isn't defined.

Conclusion

In this topic you learn about the different cases where NameError can be occurred. It’s explained with example for NameError and finally provided solution to handle NameError by exception handling.

To learn more on exception handling follow the link Python: Exception Handling.

If this blog for solving NameError help you to resolve problem make comment or if you know other way to handle this problem write in comment so that it will help others.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s