What This Error Means
This error occurs when you try to use a variable that has not been assigned a value yet or has not been defined in the current scope.
Why It Happens
In Python, you must define a variable before you can use it. If you're trying to use a variable that is declared inside a function or block of code outside of that function or block, you'll get a NameError saying the variable is not defined.
How to Fix It
- 1To fix this error, make sure to define the variable before using it. You can define a variable by assigning a value to it, like this: `total = 0`. If the variable is declared inside a function, make sure to call the function before using the variable.
Example Code Solution
total = price * quantity
print(total)total = 0
total = price * quantity
print(total)Fix for NameError: name 'total' is not defined
Browse Related Clusters
Related PYTHON Errors
RuntimeError: Cannot schedule new futures after shutdown
RuntimeError: cannot pickle local function '<locals>.<lambda>'
MemoryError: Unable to allocate 1000 bytes for an array with shape (10
CursorError: No results found for query: SELECT * FROM users WHERE id
Related PYTHON Blog Articles
Have a different error? Get an instant explanation.
Explain Another Error