Sick Gaming
[Tut] Python’s NameError: name ‘xxx’ is not defined — How to Fix This Stupid Bug? - Printable Version

+- Sick Gaming (https://www.sickgaming.net)
+-- Forum: Programming (https://www.sickgaming.net/forum-76.html)
+--- Forum: Python (https://www.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?

<div><p class="has-pale-cyan-blue-background-color has-background">The Python interpreter throws the <strong><code>NameError</code></strong> exception if it encounters an undefined variable or function name. To fix it, you must figure out why the variable is not defined—the most frequent bugs are (1) to use the variable of function name in the code <em>before </em>it was defined, or (2) to misspell the name in either the definition or the usage. </p>
<p>Have a look at the minimal example in our interactive code shell:</p>
<p> <iframe height="400px" width="100%" src="https://repl.it/@finxter/kjalfaskdfj?lite=true" scrolling="no" frameborder="no" allowtransparency="true" allowfullscreen="true" sandbox="allow-forms allow-pointer-lock allow-popups allow-same-origin allow-scripts allow-modals"></iframe> </p>
<p><em><strong>Exercise</strong>: Define variable <code>some_variable</code> before you use it and fix the bug!</em></p>
<p><strong>Note</strong>: All the explanations and solutions provided below have been verified using Python 3.8.5.</p>
<h2>Problem</h2>
<p>When one is beginning to write Python code, they will come across the <strong>NameError</strong> exception. The Python Interpreter throws this exception to state an error.  Experienced Python coders, even <a href="https://blog.finxter.com/about-guidos-fate-of-reduce-in-python-3000/" title="About Guido’s Article: “Fate of Reduce() in Python 3000”" target="_blank" rel="noreferrer noopener">Python legends like Guido</a> (I suppose), run into these errors, every now and then. In its simplest form, the error looks like something similar to the following:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">>>> print(some_variable)
Traceback (most recent call last): File "&lt;stdin>", line 1, in &lt;module>
NameError: name 'some_variable' is not defined
>>>
</pre>
<h2>Desired Output</h2>
<p>This article aims to help the reader understand some of the most common reasons for this error.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">>>> print(some_variable)
hello world
>>> </pre>
<p>The desired output, assumes the variable <strong><code>some_variable</code></strong>, points to the string <code>"<strong>hello world</strong>"</code>.  In other words, the desired output would be an error free run of the reader’s Python code.</p>
<h2>Background</h2>
<p>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 <strong>NameError</strong>.</p>
<h2>Missing Variable Definition</h2>
<p>One common cause of the <strong>NameError</strong> 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.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">>>> print(some_variable)
Traceback (most recent call last): File "&lt;stdin>", line 1, in &lt;module>
NameError: name 'some_variable' is not defined
>>>
</pre>
<p>Oops!!! The reader finds out they have not defined <strong><code>some_variable</code></strong>, before they used it! Fix this problem as shown below. Define <strong><code>some_variable</code></strong> before using it!</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">>>> some_variable = ‘Hello World’
>>> print(some_variable)
Hello World
>>> </pre>
<h2>Misspelled Variable Name</h2>
<p>Misspelled variable names can be erroneous in a similar way. Consider the following example code.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">>>> som_variable = ‘Hello World’
>>> print(some_variable)
Traceback (most recent call last): File "&lt;stdin>", line 1, in &lt;module>
NameError: name 'some_variable' is not defined
>>> </pre>
<p><strong>Note</strong>: <strong><code>som_variable</code></strong> is not the same as <strong><code>some_variable</code></strong> (i.e. missing <code>'e'</code>)</p>
<h2>Missing Function Definitions</h2>
<p>Another common cause of the <strong>NameError</strong> exception is a missing function definition. Like variable definitions, the reader should define any function, before using it. Consider the following code.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">>>> some_other_string = ‘Hello World’
>>> some_function(some_other_string)
Traceback (most recent call last): File "&lt;stdin>", line 1, in &lt;module>
NameError: name 'some_function' is not defined
>>> </pre>
<p>Again, the function <code>'some_function'</code> is not defined before its use.<br />Fix this problem as shown below. Define <code>'some_function'</code> before using it.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">>>> def some_function(some_string):
... print(some_string)
... >>> some_other_string = ‘Hello World’
>>> some_function(some_other_string)
Hello World
>>>
</pre>
<h2>Misspelled Function Name</h2>
<p>Misspelled function names can be erroneous in a similar way. Consider the following example code.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">>>> def som_function(some_string):
... print(some_string)
... >>> some_other_string = ‘Hello World’
>>> some_function(some_other_string)
Traceback (most recent call last): File "&lt;stdin>", line 1, in &lt;module>
NameError: name 'some_function' is not defined
>>> </pre>
<p><strong>Note</strong>: <code>'som_function'</code> is not the same as <code>'some_function'</code> (i.e. missing <code>'e'</code>)</p>
<h2>Wrong Scope</h2>
<p>Yet another common cause of the <strong>NameError</strong> exception is the use of the variable in the wrong scope. Consider the following example.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">>>> ## 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 "&lt;stdin>", line 1, in &lt;module>
NameError: name 'a_local_variable' is not defined
>>> </pre>
<p>The <strong>NameError</strong> exception occurred because <strong><code>a_local_variable</code></strong> got called from outside its function scope. <br />One way to fix this is by defining <strong><code>a_local_variable</code></strong> as a global variable instead. Consider the following example.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">>>> ## 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…
>>> </pre>
<h2>Unquoted String In print() Statement</h2>
<p>Forgetting to quote strings in the <code><a href="https://blog.finxter.com/the-separator-and-end-arguments-of-the-python-print-function/" title="Python Print Function [And Its SECRET Separator &amp; End Arguments]">print()</a></code> statement can cause the <strong>NameError</strong> exception. This does not happen often, but it is good to know that it can happen. The reader is more likely to see a <strong>SyntaxError</strong> rather than a <strong>NameError</strong>. Consider the following examples…</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">>>> print(Hello)
Traceback (most recent call last): File "&lt;stdin>", line 1, in &lt;module>
NameError: name 'Hello' is not defined >>> print(Hello World) File "&lt;stdin>", line 1 print(Hello World) ^
SyntaxError: invalid syntax
>>> </pre>
<p>In both the examples above, unquoted strings cause errors. <strong>NameError</strong> in one case and <strong>SyntaxError</strong> in another.&nbsp;</p>
<p>In this case, the fix is simple. Enclose the strings in quotes.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">>>> print(‘Hello’)
Hello >>> print(‘Hello World’)
Hello World
</pre>
<h2>Conclusion</h2>
<p>Such errors will happen in the reader’s coding life. The important thing is to learn from it and move on.&nbsp; 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.</p>
<h2>Finxter Academy</h2>
<p>This blog was brought to you by <strong>Girish</strong>, a student of <a href="https://blog.finxter.com/about/">Finxter Academy</a>. You can find his <a href="https://www.upwork.com/freelancers/~014e84f9a10f3d79c8">Upwork profile here</a>.</p>
<h2>References</h2>
<p>All research for this blog article was done using <a href="https://docs.python.org/3.8/">Python Documents</a>, the <a href="http://www.google.com/">Google Search Engine</a> and the shared knowledge-base of the <a href="https://blog.finxter.com/about/">Finxter Academy</a> and the <a href="https://stackoverflow.com/">Stack Overflow</a> Communities. Concepts and ideas were also researched from the <a href="https://www.bu.edu/homepage-alt/">Boston University </a>and <a href="https://careerkarma.com/">Career Karma</a> communities.</p>
<p>The post <a href="https://blog.finxter.com/pythons-nameerror/" target="_blank" rel="noopener noreferrer">Python’s NameError: name ‘xxx’ is not defined — How to Fix This Stupid Bug?</a> first appeared on <a href="https://blog.finxter.com/" target="_blank" rel="noopener noreferrer">Finxter</a>.</p>
</div>


https://www.sickgaming.net/blog/2020/10/16/pythons-nameerror-name-xxx-is-not-defined-how-to-fix-this-stupid-bug/