DEV Community

Cover image for Python Errors: Nameerror name is not defined and more
hellocodeclub
hellocodeclub

Posted on • Edited on

Python Errors: Nameerror name is not defined and more

Errors are inevitable when you are programming. As you write code, errors will start raising. The better you understand these errors, the easier it will be to avoid them. In this article you will learn the main python errors, how to interpret them and how they arise. For example, python nameerror name is not defined, what does this mean? You will find out by the end of this tutorial.

he goal of an error, or exception, is flagging something unexpected happened while running the code. Some of these situation arise frequently. Therefore python contains some built-in exceptions that capture the more frequent unexpected situation. Below we will go through each of those exception types and see what’s the meaning behind.

SyntaxError: invalid syntax

This error occurs when the code you write doesn’t follow the python syntax rule. For example, not closing a parenthesis will lead to a syntax error. The python parser won’t be able to parse the code if it doesn’t follow the syntax rule, therefore it can’t process it any further. Let’s see some examples:

Example #1

list = [1, 23, 45, 0, 9]
for item in list
    print(item)
Enter fullscreen mode Exit fullscreen mode

Output:

  File line 2
    for item in list
                   ^
SyntaxError: invalid syntax
Enter fullscreen mode Exit fullscreen mode

This code raised an unexpected situation, because line 2 is missing the colon at the end, which breaks the python syntax rules.

Example #2

list = [1, 23, 45, 0, 9]
for item in list:
    print(item
Enter fullscreen mode Exit fullscreen mode

Output:

  File line 4

                 ^
SyntaxError: unexpected EOF while parsing
Enter fullscreen mode Exit fullscreen mode

The code above raised an error because line 3 is missing the closing parenthesis.

Python Nameerror name is not defined

You will encounter a nameerror ( name is not defined) when a variable is not defined in the local or global scope. Or you used a function that wasn’t defined anywhere in your program. For example, you will see this error if you try to print a variable that wasn’t defined. You might also see this error when you use a built-in library, but forget to import the library first. Let’s see a few code examples:

Example #1

number = 1
print(num)
Enter fullscreen mode Exit fullscreen mode

Output:

Traceback (most recent call last):
  File line 4, in <module>
    print(num)
NameError: name 'num' is not defined
Enter fullscreen mode Exit fullscreen mode

Usually this error is highlighting that there is a typo in one of the variable names.

Example #2

def print_age(age):
    print('My age is: '+str(age))

print__age(14)
Enter fullscreen mode Exit fullscreen mode

Output:

Traceback (most recent call last):
  File line 4, in <module>
    print__age(14)
NameError: name 'print__age' is not defined
Enter fullscreen mode Exit fullscreen mode

This issue is similar to the previous example, but applied to function. Although there is a “print age” function, the function name is print, underscore and age, however when I called the function I used double underscore __. That’s why the code can’t find the function

Read more about KeyError, ModuleNotFoundError, AttributeError and IndexError

Find out How to Fix – no module named tkinter error

How to Fix Valueerror: too many values to unpack

Fix Typeerror a bytes-like object is required not ‘str’

I hope you enjoyed the article and thanks for reading! Happy Coding! 😃

Top comments (0)