Error guide · NameError
NameError: name is not defined
Python met a name (a variable or function) that it does not know.
Example
score = 95 print(scroe)
What Python says
NameError: name 'scroe' is not defined. Did you mean: 'score'?
As printed by Python 3.12. Other versions may word it slightly differently.
Why it happens
Names must be spelled exactly as they were created, including capital letters. The error also appears if you use a variable before the line that creates it, or forget the quotes around text, so Python thinks the text is a variable name.
How to fix it
score = 95 print(score)
Output: 95
Tips
- Check the spelling and capital letters:
Scoreandscoreare different names. - Make sure the line that creates the variable runs before the line that uses it.
- Text needs quotes:
print(hello)looks for a variable,print("hello")prints the word. - Functions from modules need the module name or an import:
math.sqrt(9)afterimport math.