Start learning free

LESSON 6 OF 673 · WELCOME TO PYTHON

Name Errors in Python

Lesson

TL ; DR:

  • A NameError occurs when Python sees a word it does not recognize. Some examples:

  • Misspelling a function name:

    prnt("Heyooo!") # NameError: name 'prnt' is not defined
    

  • Misspelling a variable name:

    animal = "parrot"
    print(anmal) # NameError: name 'anmal' is not defined
    

  • When Python encounters a name error, it immediately stops executing your code and throws a NameError instead.




Full lesson:

Humans are prone to making mistakes. Humans are also typically in charge of creating computer programs. To compensate, programming languages attempt to understand and explain mistakes made in their programs.

Here are two common errors that we encounter while writing Python code:


The NameError:

A NameError occurs when the Python interpreter sees a word it does not recognize. Some examples:

1. Misspelling a function's name:

prnt("My name is Andy")

2. Using a non-existent variable / Misspelling a variable's name:

animal = "parrot"
print(aniimal)

When we run this code we'll get a NameError: name 'aniimal' is not defined.


Assignment
Follow the Coding Tutorial and let's practice with errors!


Hint
Look at the examples above if you get stuck.


STEP 1 OF 1
    Ctrl + Enter to check