← All Python errors

Python error explained

NameError: what it means and how to fix it

You used a name Python has never seen.

The message

NameError: name 'totl' is not defined

What it means

Python looked for a variable or function with that exact name and could not find one. Names are case sensitive, so Total and total are different, and a name only exists after the line that creates it has run.

Common causes

  • A typo in the variable name
  • Different capitals, like Score and score
  • Using a variable before the line that sets it
  • Forgetting the quotes around text, so Python thinks the word is a variable

Example that raises it

prices = [3.50, 2.25, 4.00]
total = sum(prices)
print(f"Total: £{totl:.2f}")

The fix

prices = [3.50, 2.25, 4.00]
total = sum(prices)
print(f"Total: £{total:.2f}")

Tip: Copy the name from where you created it instead of retyping it. If the word was meant to be text, put it in quotes.

Fix the broken version yourself in the editor on this page. It runs real Python in your browser.

Start the free lessons More Python errors