All Python errors
NameError in Python: name is not defined
NameError means Python met a name it does not know yet. To fix it, create the variable or function above the line that uses it and check that the spelling and letter case match exactly.
Code with the error
price = 40
print(price * quantity)
Error message (Python 3.12, last line)
Corrected code
price = 40
quantity = 3
print(price * quantity)
Output
120
Common causes
- The variable is used before it gets a value.
- A typo or different letter case: Total and total are two names.
- Text without quotes: print(hello) looks for a variable called hello.
- A variable created inside a function is used outside it.
Practise in the course
Lesson: VariablesFrequently asked questions
Why do I get NameError for ordinary text?
Without quotes Python treats a word as a name. Write the text in quotes: "hello".
Does letter case matter in names?
Yes. age, Age and AGE are three different names.