{ }codelper

Error Messages › Python

Python NameError: name 'x' is not defined

NameError: name 'x' is not defined

You get this error when Python meets a name it has never seen. A variable has to be created once with name = value before you can use it, and this error means no such name had been created by the time Python reached that line. Start by checking where the name inside the quotes in the error message was created and whether it is spelled the same.

Cause 1. You mistyped the name

This is the most common cause. If even one letter is different or two letters are swapped, it is a completely different name to Python. Upper and lower case differ too, so Total and total are different names.

Wrong code

total = 0for x in [3, 5, 7]:    total = total + xprint(totla)
When you run it
Traceback (most recent call last):
  File "main.py", line 4, in <module>
NameError: name 'totla' is not defined. Did you mean: 'total'?

Edit and run it →

Fixed code

Make the name on the line that uses it exactly the same as on the line that created it. If the error message ends with Did you mean: 'total'?, Python has found a similar name for you (from Python 3.10 on).

total = 0for x in [3, 5, 7]:    total = total + xprint(total)
Output
15

Edit and run it →

Cause 2. You left the quotes off some text

A word without quotes is read as a variable name. print(welcome) means "print the value of the variable called welcome", so if there is no such variable you get this error.

Wrong code

print(welcome)
When you run it
Traceback (most recent call last):
  File "main.py", line 1, in <module>
NameError: name 'welcome' is not defined

Edit and run it →

Fixed code

To print the text as it is, wrap it in quotes to make it a string. The same goes for comparing an answer you got from input(): not if answer == yes but if answer == "yes".

print("welcome")answer = "yes"if answer == "yes":    print("ok")
Output
welcome
ok

Edit and run it →

Cause 3. You used it before giving it a value

total = total + x reads the total on the right first. If total was never created before that, there is no value to read and you get the error. It happens when you leave out the starting value for a sum or a count.

Wrong code

for x in [3, 5, 7]:    total = total + xprint(total)
When you run it
Traceback (most recent call last):
  File "main.py", line 2, in <module>
NameError: name 'total' is not defined

Edit and run it →

Fixed code

Give it a starting value before the loop. Start with 0 for a sum, "" when joining strings together, and [] when collecting values in a list.

total = 0for x in [3, 5, 7]:    total = total + xprint(total)
Output
15

Edit and run it →

It is the same when a variable is created only inside an if. If the condition is false, that line does not run and the variable is never created. Give it a starting value before the if.

Cause 4. You used a variable from inside a function outside it

A variable created inside a function lives only inside that function. It is gone when the function ends, so the code outside does not know the name.

Wrong code

def add(a, b):    result = a + b add(3, 5)print(result)
When you run it
Traceback (most recent call last):
  File "main.py", line 5, in <module>
NameError: name 'result' is not defined

Edit and run it →

Fixed code

Hand back the value you need outside with return, and store it in a variable where you call the function.

def add(a, b):    return a + b result = add(3, 5)print(result)
Output
8

Edit and run it →

What to check when you see this error

  1. Look at the name inside the quotes on the last line of the error message. That name is the problem.
  2. Find the line that created the name (name = value) and compare the spelling and the upper and lower case.
  3. The number after line in the error message is the line where the error happened. Check that the line creating the name runs before that line.
  4. If you meant to print text, check that you put quotes around it.
  5. If you used something from a module, like math.sqrt, check that import math is at the top.

Practice problems

See also