Python NameError: name is not defined
A plain-words explanation of this Python message: what it means, the usual causes, and the steps that fix it.
The messageNameError: name 'totl' is not defined
Make the error yourself
Run with Python 3.14.10. This is the smallest example we could find that shows it.
Python program (main.py)
total = 10 + 5
print(totl)Output (the real message)
Traceback (most recent call last):
File "main.py", line 2, in <module>
print(totl)
^^^^
NameError: name 'totl' is not defined. Did you mean: 'total'?Why it happens
Python looked for a name, a variable, function or module, and found nothing with that exact spelling at that point in the program. The usual causes are, in order: a typo (the example spells total as totl), using the name before the line that creates it, a name created inside a function and used outside it, and forgetting to import a module. Python is case sensitive, so Total and total are different names.
Newer Python versions add a hint such as "Did you mean: 'total'?" under the message. Read it: it is right most of the time.
How to fix it
- Read the name in the message and look at the line number in the traceback. Compare the spelling with where you created the variable.
- Check the order. The line that creates the variable must run before the line that uses it.
- If the name was created inside a function, return it (
return total) and store the result where you call the function. - If it is a module (
math,json,os), addimport mathat the top of the file.
The corrected version
Python program (main.py)
total = 10 + 5
print(total)Output (the corrected version)
15How to avoid it
Use an editor that underlines unknown names, and choose short but clear names. Run the program often while you write it, so a typo is found within a minute and not within an hour.
Questions
Why does the same name work in one function and fail in another?
Variables created inside a function are local to it. Pass values in as arguments and send results out with return.
This is an independent guide. It is not connected with Python, Microsoft, Google, GitHub or the Git project. Product names belong to their owners. Examples were run on 13 November 2026 in a practice folder or a blank workbook; your version may word a message slightly differently.