Codementor Events

Python Errors: Nameerror name is not defined and more

Published Sep 09, 2020Last updated Sep 28, 2021
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.

The 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.

See a list of all built-in errors in the python documentation

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)

Output:

  File line 2
    for item in list
                   ^
SyntaxError: invalid syntax

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

Output:

  File line 4
   
                 ^
SyntaxError: unexpected EOF while parsing

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)

Output:

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

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)

Output:

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

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! 😃

Discover and read more posts from Marta Rey
get started
post commentsBe the first to share your opinion
Beginner
2 years ago

I am Getting this type of error: if Question123 == “yes”:
NameError: name ‘Question123’ is not defined
This Is My Code:
elif’remember that jarvis’ in query:
rememberMsg = query.replace(“remember that”,"")
rememberMsg = rememberMsg.replace(“jarvis”,"")
speak(“You Told Me To Remind You :”+rememberMsg)
print(“You Told Me To Remind You :”+rememberMsg)
Question123 = input ("Enter yes or no: ")

    if Question123 == "yes": 
       remember = open('data.txt','a')
       remember.write('\n'+rememberMsg)
       remember.close()
       print('Ok')
       speak('Ok')

    elif Question123 == ("no"): 
         print('Ok, I Will Not Remember That')
         speak('Ok, I Will Not Remember That')

    elif ('what do you remember') in query:
        remember = open('data.txt','r')
        speak("You Told Me That" + remember.read())
        print("You Told Me That" + remember.read())
Nour islem Ben azaiez
3 years ago

Hi ,I read your article and it was indeed very helpful however I still couldn’t find a solution to my problem . I wrote this code:
cf=input(‘couleur du feu(r,v,o)?’)
while cf!=r and cf!=v and cf!=o :
cf=input('couleur du feu (r,v,o)? ')
if cf ==r or cf==v or cf==o :
break;
p=input("0:pas de pieton ;1: il y a un pieton ")
while p!=0 and p!=1 :
p=input("0:pas de pieton ;1: il y a un pieton ")
if p==0 or p==1 :
break;
if p==1 or cf==r :
print(“action = s’arreter “)
elif cf==o :
print(” action = ralentir”)
else :
print("action = passer ")
(But it said nameerror : name ‘r’ not defined . Could you please tell me what should I change 🙏🏻

Marta Rey
3 years ago

Hi Nour Islem, Thanks for your comment! The problem in your code is that characters should be within single or double quotes. For example instead of:

while cf!=r and cf!=v and cf!=o :

it should look like this

while cf!='r' and cf!='v' and cf!='o' :

Also just to make your life easier, I will recommend using variable with readable names instead of letters. It makes the code easier to follow.

In case you would like to read more about how to make your code more readable and easy to manage, please feel to check out this article:

https://www.hellocodeclub.com/java-clean-code-4-simple-tricks-to-write/

The tricks can be applied to python as well :)

I hope this helps! and best luck learning Python!

Show more replies