PYTHONWarningReference ErrorMay 4, 2026

Reference Error

NameError: name 'total' is not defined

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

  1. 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

❌ Before (problematic code)
Python
total = price * quantity
print(total)
✅ After (fixed code)
Python
total = 0
total = price * quantity
print(total)

Fix for NameError: name 'total' is not defined

Related PYTHON Errors

Related PYTHON Blog Articles

Have a different error? Get an instant explanation.

Explain Another Error