![]() |
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. 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 Undefined Variables are UsedAs 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
Output: Traceback (most recent call last): When there is a Spelling MistakeNow 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
Output: Traceback (most recent call last): A similar error will arise even if we misspell any in-built function like print() or input(). Python3
Output: Traceback (most recent call last): When the Variable is Accessed First and Defined LaterIn 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
Output: Traceback (most recent call last): When a Variable is used out of its defined ScopeIn 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
Output: Traceback (most recent call last): How to Solve an Undefined Variable NameError in Python?To solve the NameError: name ‘var’ is not defined, you should take care of the pointers mentioned above. The previous examples can be updated to get rid of the error as follows: Define the Variable before using itMake sure to define a variable before you use it: Python3
Output
GeeksforGeeks! GFG Avoid Spelling Mistakes in Variable Names and Function NamesMake sure you do not misspell any variable name: Python3
Output
GeeksforGeeks! Make sure you do not misspell any in-built function or user-defined function: Python3
Output: What is your age?20 Access a Variable only when its first definedMake sure you first create the variable and then use it and not the other way around: Python3
Output
GeeksforGeeks Access the Variable in its defined scopeMake sure you do not access a variable out of its defined scope. Python3
Output
GeeksforGeeks GFG Apart from taking care of all these pointers, you can also setup the try-else block to resolve this error in a more efficient manner. Read all about it here – Handling NameError Exception in Python. ConclusionIn this post, we looked at the different reasons that might lead to an undefined variable nameerror in Python. Although in most cases this error is thrown because of a wrong spelling, the use of scope is also often overlooked, which naturally leads to the nameerror. Although avoiding this error is the best that you can do, especially now that you are aware of the scenarios that lead to it, exploring the use of exception handling is also a great option to save you from this error for once and for all. |
Reffered: https://www.geeksforgeeks.org
Geeks Premier League |
Related |
---|
![]() |
![]() |
![]() |
![]() |
![]() |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 13 |