www.freecodecamp.org Open in urlscan Pro
172.67.70.149  Public Scan

URL: https://www.freecodecamp.org/news/nameerror-name-plot_cases_simple-is-not-defined-how-to-fix-this-python-error/
Submission: On October 10 via api from AU — Scanned from AU

Form analysis 1 forms found in the DOM

<form id="search-form" data-test-label="search-bar">
  <div role="search" class="searchbox__wrapper">
    <label class="fcc_sr_only" for="search-input"> Search </label>
    <input type="search" placeholder="
    Search 10,400+ tutorials
" id="search-input" class="aa-input" autocomplete="off" spellcheck="false" role="combobox" aria-autocomplete="list" aria-expanded="false" aria-owns="algolia-autocomplete-listbox-0" dir="auto" style="">
    <pre aria-hidden="true"
      style="position: absolute; visibility: hidden; white-space: pre; font-family: Lato, sans-serif; font-size: 18px; font-style: normal; font-variant: normal; font-weight: 400; word-spacing: 0px; letter-spacing: normal; text-indent: 0px; text-rendering: auto; text-transform: none;"></pre>
    <button type="submit" class="ais-SearchBox-submit">
      <svg class="ais-SearchBox-submitIcon" xmlns="https://www.w3.org/2000/svg" width="10" height="10" viewBox="0 0 40 40">
        <path
          d="M26.804 29.01c-2.832 2.34-6.465 3.746-10.426 3.746C7.333 32.756 0 25.424 0 16.378 0 7.333 7.333 0 16.378 0c9.046 0 16.378 7.333 16.378 16.378 0 3.96-1.406 7.594-3.746 10.426l10.534 10.534c.607.607.61 1.59-.004 2.202-.61.61-1.597.61-2.202.004L26.804 29.01zm-10.426.627c7.323 0 13.26-5.936 13.26-13.26 0-7.32-5.937-13.257-13.26-13.257C9.056 3.12 3.12 9.056 3.12 16.378c0 7.323 5.936 13.26 13.258 13.26z">
        </path>
      </svg>
      <span class="fcc_sr_only">Submit your search query</span>
    </button>
    <div id="dropdown-container"><span class="algolia-autocomplete" style="position: absolute; z-index: 100; display: none; direction: ltr;"><span class="aa-dropdown-menu" role="listbox" id="algolia-autocomplete-listbox-0"
          style="display: block; left: 0px; right: auto;">
          <div class="aa-dataset-1"></div>
        </span></span></div>
  </div>
</form>

Text Content

Search



Submit your search query


Forum Donate

Learn to code — free 3,000-hour curriculum


June 27, 2022 / #Python


NAMEERROR: NAME PLOT_CASES_SIMPLE IS NOT DEFINED – HOW TO FIX THIS PYTHON ERROR

Ihechikara Vincent Abba

This first step in fixing a coding error is to understand the error. Although
some error messages may seem confusing, most of them will help you fix the
error.

In this article, we'll be talking about an error that falls under the NameError
category in Python.

You'll see what a NameError is, some code examples to show how/why the error
occurs, and how to fix them.


WHAT IS A NAMEERROR IN PYTHON?

In Python, the NameError occurs when you try to use a variable, function, or
module that doesn't exist or wasn't used in a valid way.

Some of the common mistakes that cause this error are:

 * Using a variable or function name that is yet to be defined.
 * Misspelling a variable/function name when calling the variable/function.
 * Using a Python module without importing the module, and so on.


HOW TO FIX "NAMEERROR: NAME IS NOT DEFINED" IN PYTHON

In this section, you'll see how to fix the "NameError: Name is Not Defined"
error in Python.

I've divided this section into sub-sections to show the error above when using
variables, functions, and modules.

We'll start with code blocks that raise the error and then see how to fix them.


EXAMPLE #1 - VARIABLE NAME IS NOT DEFINED IN PYTHON

name = "John"

print(age)
# NameError: name 'age' is not defined

In the code above, we defined a name variable but tried to print age which is
yet t0 be defined.

We got an error that says: NameError: name 'age' is not defined to show that the
age variable doesn't exist.

To fix this, we can create the variable and our code will run fine. Here's how:

name = "John"
age = 12

print(age)
# 12

Now the value of age gets printed out.

Similarly, the same error can be raised when we misspell a variable name. Here's
an example:

name = "John"

print(nam)
# NameError: name 'nam' is not defined

In the code above, we wrote nam instead of name.  To fix errors like this, you
just have to spell the variable name the right way.


EXAMPLE #2 - FUNCTION NAME IS NOT DEFINED IN PYTHON

def sayHello():
    print("Hello World!")
    
sayHelloo()
# NameError: name 'sayHelloo' is not defined

In the example above, we added an extra o while calling the function —
sayHelloo() instead of sayHello().

We got the error: NameError: name 'sayHelloo' is not defined. Spelling errors
like this are very easy to miss. The error message usually helps in fixing this.

Here's the right way to call the function:

def sayHello():
    print("Hello World!")
    
sayHello()
# Hello World!


Just like we saw in the previous section, calling a variable that is yet to be
defined raises an error. The same applies to functions.

Here's an example:

def sayHello():
    print("Hello World!")
    
sayHello()
# Hello World!

addTWoNumbers()
# NameError: name 'addTWoNumbers' is not defined

In the code above, we called a function – addTWoNumbers() – that was yet to be
defined in the program. To fix this, you can create the function if you need it
or just get rid of the function if it is irrelevant.

Note that calling a function before creating it will throw the same error your
way. That is:

sayHello()

def sayHello():
    print("Hello World!")
    
# NameError: name 'sayHello' is not defined

So you should always define your functions before calling them.


EXAMPLE #3 - USING A MODULE WITHOUT IMPORTING THE MODULE ERROR IN PYTHON

x = 5.5

print(math.ceil(x))
# NameError: name 'math' is not defined

In the example above, we're making use of the Python math.ceil method without
importing the math module.

The resulting error was this: NameError: name 'math' is not defined. This
happened because the interpreter did not recognize the math keyword.

Along with other math methods in Python, we must first import the math module to
use it.

Here's a fix:

import math

x = 5.5

print(math.ceil(x))
# 6

In the first line of the code, we imported the math module. Now, when you run
the code above, you should have 6 returned.


SUMMARY

In this article, we talked about the "NameError: Name is Not Defined" error in
Python.

We first defined what a NameError is in Python.

We then saw some examples that could raise a NameError when working with
variables, functions, and modules in Python. Each example, divided into
sections, showed how to fix the errors.

Happy coding!

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT


--------------------------------------------------------------------------------

Ihechikara Vincent Abba

ihechikara.com

--------------------------------------------------------------------------------

If you read this far, thank the author to show them you care. Say Thanks

Learn to code for free. freeCodeCamp's open source curriculum has helped more
than 40,000 people get jobs as developers. Get started

ADVERTISEMENT


freeCodeCamp is a donor-supported tax-exempt 501(c)(3) charity organization
(United States Federal Tax Identification Number: 82-0779546)

Our mission: to help people learn to code for free. We accomplish this by
creating thousands of videos, articles, and interactive coding lessons - all
freely available to the public. We also have thousands of freeCodeCamp study
groups around the world.

Donations to freeCodeCamp go toward our education initiatives, and help pay for
servers, services, and staff.

You can make a tax-deductible donation here.

Trending Guides
JS isEmpty Equivalent Submit a Form with JS Add to List in Python Grep Command
in Linux String to Int in Java Add to Dict in Python Java For Loop Example
Matplotlib Figure Size Database Normalization Nested Lists in Python
Coalesce SQL Python join() JS POST Request JS Type Checking Read Python File
SOLID Principles Sort a List in Java For Loops in Python JavaScript 2D Array SQL
CONVERT Function
Rename Column in Pandas Delete a File in Python K-Nearest Neighbors Algo iferror
Function in Excel Remove From String Python
Create a File in Terminal Clear Formatting in Excel Accounting Num Format Excel
Check if File Exists Python Iterate Over Dict in Python
Our Charity

About Alumni Network Open Source Shop Support Sponsors Academic Honesty Code of
Conduct Privacy Policy Terms of Service Copyright Policy