NameError in Python

by Alex
NameError in Python

The NameError is one of the most common errors in Python. It can be intimidating for beginners, but there is nothing complicated about it. This error tells you that you tried to use a variable that does not exist. In this tutorial, we’ll talk about the “NameError name is not defined” error. Let’s look at some examples and figure out how to solve this error.

What is a NameError?

NameError occurs when you try to use a non-existent variable name or function. In Python, code is run from top to bottom. This means that you cannot declare a variable after it has already been used. Python will simply not know it exists. The most common NameError looks like this:

NameError: name 'some_name' is not defined

Let’s break down the frequent causes of this error.

The reason #1: a mistake in the spelling of the name of the variable or function

It is easy enough for a person to make a typo. It is also easy for him to find it. But it is not that easy for Python. The language is only capable of interpreting names that were entered correctly. This is why it is important to make sure that all names in your code are entered correctly. If an error is not corrected, an exception will occur. Take the following code as an example:


books = ["Near Dark", "The Order", "Where the Crawdads Sing"]
print(boooks)

It will return:

Traceback (most recent call last):
File "main.py", line 3, in
print(boooks)
NameError: name 'boooks' is not defined

The typo needs to be corrected to solve the problem. If you type print(books), the code will return a list of books. Thus, if there is a naming error, the first thing to check is that all variable and function names are entered correctly.

The reason #2: Calling a function before the declaration

Functions should be used after the declaration, similar to variables. This is because Python reads code from top to bottom. Let’s write a program that calls a function before the declaration:


books = ["Near Dark", "The Order", "Where the Crawdads Sing"]
print_books(books)
def print_books(books):
for b in books:
print(b)

The code will return:

Traceback (most recent call last):
File "main.py", line 3, in
print_books(books)
NameError: name 'print_books' is not defined

On line 3 we try to call print_books(). However, this function is declared later. To fix this error, we need to move the function above:


def print_books(books):
for b in books:
print(b)
books = ["Near Dark", "The Order", "Where the Crawdads Sing"]
print_books(books)

Reason #3: The variable is not declared

Programs get larger and sometimes it is easy to forget to declare a variable. In this case an error will occur. The reason is that Python cannot handle undeclared variables. Let’s look at a program that outputs a list of books:


for b in books:
print(b)

Such code will return:

Traceback (most recent call last):
File "main.py", line 1, in
for b in books:
NameError: name 'books' is not defined

The books variable was not declared. To solve the problem, the variable must be declared in the code:


books = ["Near Dark", "The Order", "Where the Crawdads Sing"]
for b in books:
print(b)

Reason #4: Trying to print one word

To output a single word, we need to put it in double brackets. This is how we tell Python that it is a string. If we don’t, the language will assume it is part of a program. Consider an instruction like print():


print(Books)

This code tries to print the word “Books” to the console. Instead it will return an error:

Traceback (most recent call last):
File "main.py", line 1, in
print(Books)
NameError: name 'Books' is not defined

Python treats “Books” as a variable name. To solve the problem you need to put the name in brackets:


print("Books")

Python now knows to output a string to the console, and the code returns Books.

Reason #5: Declaring a variable out of scope

There are two scopes of variables: local and global. Local variables are accessible within functions or classes where they have been declared. Global variables are available throughout the entire program. If you try to access a local variable outside its scope, an error will occur. The following code tries to display a list of books along with their total number:


def print_books():
books = ["Near Dark", "The Order", "Where the Crawdads Sing"]
for b in books:
print(b)
print(len(books))

The code returns:

Traceback (most recent call last):
File "main.py", line 5, in
print(len(books))
NameError: name 'books' is not defined

The books variable was declared, but it was declared inside print_books(). This means it cannot be accessed in the rest of the program. To solve this problem you need to declare the variable in the global scope:


books = ["Near Dark", "The Order", "Where the Crawdads Sing"]
def print_books():
for b in books:
print(b)
print(len(books))

The code prints the name of each book in the books list. After that the total number of books in the list is output using the len() method.

Related Posts

LEAVE A COMMENT