ShareYourGroupShareYourGroupPython

NameError: name is not defined

A NameError means Python met a name it does not know. Every name, whether a variable, a function or a module, must be given a value before it is used. The message tells you exactly which name was the problem.

total = 50
print(totl)

What Python says

Traceback (most recent call last):
  File "example.py", line 2, in <module>
    print(totl)
          ^^^^
NameError: name 'totl' is not defined. Did you mean: 'total'?

Unlike a syntax error, this one appears while the program is running. The lines above the bad one have already run.

Cause 1: a spelling mistake

This is the reason nine times out of ten. Compare the name in the message, letter by letter, with the name where you created the variable. Remember that capital letters count: Total, total and TOTAL are three different names. Recent versions of Python help by suggesting the name you may have meant, as in the example above. The suggestion is only a guess from similar spellings. It can be wrong, as two of the examples below show, so read it as a hint and not as the answer.

Cause 2: text without quotation marks

print(hello)

What Python says

Traceback (most recent call last):
  File "example.py", line 1, in <module>
    print(hello)
          ^^^^^
NameError: name 'hello' is not defined. Did you mean: 'help'?

Without quotation marks, hello is not text. Python takes it for a variable name, looks for it and finds nothing. If you meant the word itself, write print("hello").

Cause 3: using a name before it is created

Python runs from top to bottom. A variable does not exist until the line that assigns it has run.

print(price)
price = 120

What Python says

Traceback (most recent call last):
  File "example.py", line 1, in <module>
    print(price)
          ^^^^^
NameError: name 'price' is not defined. Did you mean: 'print'?

Move the assignment above the line that uses it. The same applies to functions: the def must come before the first call.

Cause 4: a variable made inside a function

A variable created inside a function exists only inside that function.

def work_out():
    answer = 42

work_out()
print(answer)

What Python says

Traceback (most recent call last):
  File "example.py", line 5, in <module>
    print(answer)
          ^^^^^^
NameError: name 'answer' is not defined

To get a value out of a function, return it and store the result: answer = work_out(). The lesson on arguments and scope explains this rule.

Cause 5: a forgotten import

print(math.sqrt(16))

What Python says

Traceback (most recent call last):
  File "example.py", line 1, in <module>
    print(math.sqrt(16))
          ^^^^
NameError: name 'math' is not defined. Did you forget to import 'math'?

Here the unknown name is math. Modules must be imported before use. Add import math at the top of the file.

Cause 6: a variable set only inside an if

marks = 20
if marks >= 33:
    result = "pass"
print(result)

What Python says

Traceback (most recent call last):
  File "example.py", line 4, in <module>
    print(result)
          ^^^^^^
NameError: name 'result' is not defined

The condition was false, so the line that creates result never ran. Give the variable a starting value before the if, or add an else that sets it.

How to track it down

  1. Read the name in the last line of the message.
  2. Search your file for that exact spelling. If it appears only once, it is a typing slip.
  3. If it is spelt right, check that the line creating it runs before the line using it, and that both are in the same function.

A related error, UnboundLocalError, appears when a function assigns to a name somewhere in its body and reads it before that assignment. The fix is the same idea: give the name a value first, or pass the value in as a parameter.

Other error messages

Examples on this page were run with Python 3.14 on 12 November 2026. The output shown is the real output.

This is an independent learning site. It is not connected with or approved by the Python Software Foundation. "Python" is a trademark of the Python Software Foundation and is used here only to name the language.