![]() |
|
[Tut] Python’s NameError: name ‘xxx’ is not defined — How to Fix This Stupid Bug? - Printable Version +- Sick Gaming (https://sickgaming.net) +-- Forum: Programming (https://sickgaming.net/forum-76.html) +--- Forum: Python (https://sickgaming.net/forum-83.html) +--- Thread: [Tut] Python’s NameError: name ‘xxx’ is not defined — How to Fix This Stupid Bug? (/thread-97795.html) |
[Tut] Python’s NameError: name ‘xxx’ is not defined — How to Fix This Stupid Bug? - xSicKxBot - 10-16-2020 Python’s NameError: name ‘xxx’ is not defined — How to Fix This Stupid Bug? The Python interpreter throws the Code: NameErrorHave a look at the minimal example in our interactive code shell: https://repl.it/@finxter/kjalfaskdfj?lite=true Exercise: Define variable Code: some_variableNote: All the explanations and solutions provided below have been verified using Python 3.8.5. Problem When one is beginning to write Python code, they will come across the NameError exception. The Python Interpreter throws this exception to state an error. Experienced Python coders, even Python legends like Guido (I suppose), run into these errors, every now and then. In its simplest form, the error looks like something similar to the following: Code: >>> print(some_variable)
Traceback (most recent call last): File "<stdin>", line 1, in <module>
NameError: name 'some_variable' is not defined
>>>This article aims to help the reader understand some of the most common reasons for this error. Code: >>> print(some_variable)
hello world
>>>The desired output, assumes the variable Code: some_variableCode: "hello world"Background Python is an interpreted language. This means, it interprets any Python code, line by line, from the beginning of the code to the end. The execution usually stops at the first error which the Python Interpreter encounters. The error message usually prints out helpful information about the problem. In most cases, the reader can debug, deduce and locate the erroneous syntax and fix it. This blog will attempt to describe one such common problem called the NameError. Missing Variable Definition One common cause of the NameError exception is a missing variable definition. As mentioned before, Python is an interpreted language. This means that the reader should define the variables before using them. Consider the following code. The reader is eager to try out some basic Python code real quick. So they fire up the Python interpreter to try out their fresh Python skills. Code: >>> print(some_variable)
Traceback (most recent call last): File "<stdin>", line 1, in <module>
NameError: name 'some_variable' is not defined
>>>Oops!!! The reader finds out they have not defined Code: some_variableCode: some_variableCode: >>> some_variable = ‘Hello World’
>>> print(some_variable)
Hello World
>>>Misspelled variable names can be erroneous in a similar way. Consider the following example code. Code: >>> som_variable = ‘Hello World’
>>> print(some_variable)
Traceback (most recent call last): File "<stdin>", line 1, in <module>
NameError: name 'some_variable' is not defined
>>>Note: Code: som_variableCode: some_variableCode: 'e'Missing Function Definitions Another common cause of the NameError exception is a missing function definition. Like variable definitions, the reader should define any function, before using it. Consider the following code. Code: >>> some_other_string = ‘Hello World’
>>> some_function(some_other_string)
Traceback (most recent call last): File "<stdin>", line 1, in <module>
NameError: name 'some_function' is not defined
>>>Again, the function Code: 'some_function'Fix this problem as shown below. Define Code: 'some_function'Code: >>> def some_function(some_string):
... print(some_string)
... >>> some_other_string = ‘Hello World’
>>> some_function(some_other_string)
Hello World
>>>Misspelled function names can be erroneous in a similar way. Consider the following example code. Code: >>> def som_function(some_string):
... print(some_string)
... >>> some_other_string = ‘Hello World’
>>> some_function(some_other_string)
Traceback (most recent call last): File "<stdin>", line 1, in <module>
NameError: name 'some_function' is not defined
>>>Note: Code: 'som_function'Code: 'some_function'Code: 'e'Wrong Scope Yet another common cause of the NameError exception is the use of the variable in the wrong scope. Consider the following example. Code: >>> ## Define the function some_function()
>>> def some_function():
... a_local_variable = ‘I am Local…’
... print("Printing a Local variable from within a function definition: " + a_local_variable)
... >>> ## Call some_function()
>>> some_function()
Printing a Local variable from within a function definition: I am Local...
>>> >>> ## Try to print "a_local_variable" from outside the function definition
>>> print("Attempting to print the variable from outside some_function(): " + a_local_variable)
Traceback (most recent call last): File "<stdin>", line 1, in <module>
NameError: name 'a_local_variable' is not defined
>>>The NameError exception occurred because Code: a_local_variableOne way to fix this is by defining Code: a_local_variableCode: >>> ## Define a Global Variable
>>> a_global_variable = ‘I am global…’
>>> >>> ## Define the function some_function()
>>> def some_function():
... print("Printing the Global variable from within a function definition: " + a_global_variable)
... >>> ## Call some_function()
>>> some_function()
Printing the Global variable from within a function definition: I am global...
>>> >>> ## Try to print "a_global_variable" from outside the function definition
>>> print("Attempting to print the Global variable from outside some_function(): " + a_global_variable)
Attempting to print the Global variable from outside some_function(): I am global…
>>>Forgetting to quote strings in the Code: print()Code: >>> print(Hello)
Traceback (most recent call last): File "<stdin>", line 1, in <module>
NameError: name 'Hello' is not defined >>> print(Hello World) File "<stdin>", line 1 print(Hello World) ^
SyntaxError: invalid syntax
>>>In both the examples above, unquoted strings cause errors. NameError in one case and SyntaxError in another. In this case, the fix is simple. Enclose the strings in quotes. Code: >>> print(‘Hello’)
Hello >>> print(‘Hello World’)
Hello WorldSuch errors will happen in the reader’s coding life. The important thing is to learn from it and move on. Over time the reader will get better at coding, as they incorporate good coding habits. Such errors happen lesser and lesser as the reader gets more experienced. Finxter Academy This blog was brought to you by Girish, a student of Finxter Academy. You can find his Upwork profile here. References All research for this blog article was done using Python Documents, the Google Search Engine and the shared knowledge-base of the Finxter Academy and the Stack Overflow Communities. Concepts and ideas were also researched from the Boston Universityand Career Karma communities. The post Python’s NameError: name ‘xxx’ is not defined — How to Fix This Stupid Bug? first appeared on Finxter. |