Why Does the Python NameError: name ‘var’ is not defined Arise?

Various reasons might lead to the Python NameError: name ‘var’ is not defined. Let us have a look at these common reasons:

  • When an undefined variable is used
  • When there is an spelling mistake in the variable or function name
  • When the variable is accessed first but defined later
  • When the variable is used out of its defined scope

When Undefined Variables are Used

As already mentioned, the NameError: name ‘var’ is not defined signifies that the variable we are trying to access is not defined. In other words, this error occurs when the Python interpreter fails to find a variable that we are trying to access. Here is a simple example of the same that leads to the NameError: name ‘var’ is not defined. In the below example, we get the NameError: name ‘var’ is not defined simply because we are trying to access the variable ‘b’ when it is not even defined in the program:

Python3




a = 'w3wiki!'
print(a)
 
#note that we have not declared the variable b
print(b)


Output:

Traceback (most recent call last):
File "Solution.py", line 5, in <module>
print(b)
NameError: name 'b' is not defined

When there is a Spelling Mistake

Now look at the code given below. Here, we first define the variable, ‘name_1’ and then access it using the print statement. But still, we run into the NameError: name ‘var’ is not defined. Python, being a strictly typed language, treats ‘name_1’ and ‘name_I’ as two unique variables. Thus, due to this slight mistype, the Python interpreter tries to access the variable ‘name_I’, which naturally it is unable to find. And hence, we get the NameError: name ‘name_I’ is not defined.

Python3




#define the variable
name_1 = 'w3wiki!'
 
#access the variable
print(name_I)


Output:

Traceback (most recent call last):
File "Solution.py", line 2, in <module>
print(name_I)
NameError: name 'name_I' is not defined

A similar error will arise even if we misspell any in-built function like print() or input().

Python3




x = int(inpet("What is your age?"))


Output:

Traceback (most recent call last):
File "Solution.py", line 1, in <module>
x = int(inpet("What is your age?"))
NameError: name 'inpet' is not defined

When the Variable is Accessed First and Defined Later

In this code, the variable is declared and accessed without any error in the spelling, yet, we get the NameError: name ‘var’ is not defined. This happens because Python interpreter executes the code line by line. Meaning, when the print statement in the above code runs, the presence of the variable ‘a’ is not known by the interpreter and thus, it throws the NameError:

Python3




print(a)
a = 'w3wiki'


Output:

Traceback (most recent call last):
File "Solution.py", line 1, in <module>
print(a)
NameError: name 'a' is not defined

When a Variable is used out of its defined Scope

In the code given below, we have defined a variable inside the function demo(). But, we use the print statement outside this function to access the variable ‘a’. On running this code, you will find that the code throws the NameError: name ‘var’ is not defined. This happens because the variable ‘a’ is defined inside the demo() function and hence, its scope is limited to that function only:

Python3




def demo():
  a = 'w3wiki'
   
demo()
print(a)


Output:

Traceback (most recent call last):
File "Solution.py", line 5, in <module>
print(a)
NameError: name 'a' is not defined

Undefined Variable Nameerror In Python

Encountering the “NameError: name ‘var’ is not defined” is a common experience for Python users. In Python, variable declaration is a straightforward process of assigning a value. This error arises when attempting to access a non-existent variable or one not defined in the program. Surprisingly, it can occur even when the variable is present, owing to various reasons. In this discussion, we explore the nuances of the NameError and strategies to resolve this issue in Python programming.

Similar Reads

Why Does the Python NameError: name ‘var’ is not defined Arise?

Various reasons might lead to the Python NameError: name ‘var’ is not defined. Let us have a look at these common reasons:...

How to Solve an Undefined Variable NameError in Python?

...

Contact Us