Nameerror name is not defined Python : what is it, causes and how to resolve

NameErrors are one of the most common types of Python errors. To clarify, when you are first getting started, these errors can seem intimidating. But, they are not too complicated. A NameError means that you’ve tried to use a variable that does not yet exist. In this guide, based on the documents from Careerkarma, we’re going to talk about the nameerror name is not defined Python: “nameerror name is not defined” error, why it is raised and how to reslove it.

Nameerror name is not defined Python

What is a NameError?

A NameError is raised when you try to use a variable or a function name that is not valid.

In Python, code runs from top to bottom. This means that you cannot declare a variable after you try to use it in your code. Python would not know what you wanted the variable to do.

The most common NameError looks like this:

1 nameerror name is not defined

Let’s analyze a few causes of nameerror name is not defined Python.

1. Misspelled Variable or Function Name

It’s easy for humans to gloss over spelling mistakes. To clarify, we can easily tell what a word is supposed to be even if it is misspelled. Python does not have this capability.

In addition, Python can only interpret names that you have spelled correctly. Because when you declare a variable or a function, Python stores the value with the exact name you have declared.

If there is a typo anywhere that you try to reference that variable, an error will be returned. So that, consider the following code snippet:

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


The code returns:
1 Traceback (most recent call last):
2 File "main.py", line 3, in <module>
3 print(boooks)
4 NameError: name 'boooks' is not defined

To solve this problem, all we have to do is fix the typo. If we use “print(books)”, our code returns:

1 ["Near Dark", "The Order", "Where the Crawdads Sing"]

If you receive a name error, you should first check to make sure that you have spelled the variable or function name correctly.

2. Calling a Function Before Declaration

Functions must be declared before they are used, like variables. This is because Python reads code from top-to-bottom. 

Let’s write a program that calls a function before it is declared:

1 books = ["Near Dark", "The Order", "Where the Crawdads Sing"]
2
3 print_books(books)
4
5 def print_books(books):
6   for b in books:
7       print(b)

The code returns:

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

We are trying to call print_books() on line three. However, we do not define this function until later in our program. To fix this error, we can move our function declaration to a place before we use it:

1 def print_books(books):
2   for b in books:
3       print(b)
4
5 books = ["Near Dark", "The Order", "Where the Crawdads Sing"]
6
7 print_books(books)

The code returns:

1 Near Dark
2 The Order
3 Where the Crawdads Sing

The code has successfully printed out the list of books.

3. nameerror name is not defined python: Forget to Define a Variable

As programs get larger, it is easy to forget to define a variable. If you do, a name error is raised. This is because Python cannot work with variables until they are declared.

Let’s take a look at a program that prints out a list of books:

1 for b in books:
2 print(b)

the code returns: 

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

We have not declared a variable called “books”. To solve this problem, we need to declare “books” before we use it in our code:

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

Let’s try to run our program again and see what happens:

1 Near Dark
2 The Order
3 Where the Crawdads Sing

Now that we have defined a list of books, Python can print out each book from the list

4. Try to Print a Single Word

To print out a word in Python, you need to surround it in either single or double quotes. This tells Python that a word is a string. If a word is not surrounded by quotes, it is treated as part of a program. Consider the following print() statement:

1 print(Books)

This code tries to print the word “Books” to the console. The code returns an error:

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

Python treats “Books” like a variable name. To solve this error, we can enclose the word “Books” in quotation marks:

1 print("Books")

Python now knows that we want to print out a string to the console.

New code returns: Books.

5. Declaring a Variable Out of Scope

There are two variable scopes: local and global.

Local variables are only accessible in the function or class in which they are declared. Global variables are accessible throughout a program.

If you try to access a local variable outside the scope in which it is defined, an error is raised.

The following code should print out a list of books followed by the number of books in the list:

1 def print_books():
2 books = ["Near Dark", "The Order", "Where the Crawdads Sing"]
3 for b in books:
4    print(b)
5
6 print_books()
7 print(len(books))

The code returns:

1 Near Dark
2 The Order
3 Where the Crawdads Sing
4 Traceback (most recent call last): 
5  File "main.py", line 6, in <module>
6   print(len(books))
7 NameError: name 'books' is not defined

Our code successfully prints out the list of books. On the last line of our code, an error is returned. While we have declared the variable “books”, we only declared it inside our print_books() function. This means the variable is not accessible to the rest of our program.

To solve this problem, we can declare books in our main program. This will make it a global variable:

1 books = ["Near Dark", "The Order", "Where the Crawdads Sing"]
2
3 def print_books():
4 for b in books:
5   print(b)
6
7 print_books()
8 print(len(books))

Our code returns:

1 Near Dark
2 The Order 
3 Where the Crawdads Sing
4
3
5
6
Our code prints out every book in the "books" list. Then, our code prints out the number of books in the list using the len() method.

To Sum Up With “nameerror name is not defined python”

To summarise, these errors can seem intimidating. But, they are not too complicated.  So, we hope this article about nameerror name is not defined Python will help you. Comment your thought below!

Any help with website development services? Let’s ArrowHiTech help you!

If you’re considering web development services, you can explore more about us – ArrowHiTech. Above all, we are proud to be one of the most well-established outsourcing companies all around the world. In addition, with over 12 years of experience, we can provide the best Web app development services for your business.

ArrowHiTech services

So, if you have any questions, don’t hesitate to contact us via:

Tags

Share