Python · Lesson 0.2

Reading error messages

Errors are Python telling you what went wrong and where. Learn to read the four you'll see most.

Everyone's code breaks, all the time. The skill is reading the message: it names the line and the kind of problem.

1NameError: Python doesn't know that name

A NameError usually means a typo, or using a name before you created it.

total_sales = 250
print(total_sales)
print(total_sale)  # error on purpose: the name is missing an "s"
Python

Practice

Fix the typo so the code prints 1250.

monthly_revenue = 1250
print(monthly_revnue)
Python

2SyntaxError: Python can't read the line

A SyntaxError means the code isn't written the way Python expects, like a missing bracket or quote. Python points at the line where it got confused.

print("Hello"   # error on purpose: the closing bracket is missing
Python

Practice

Fix the line so it prints Report ready.

print("Report ready)
Python

3IndentationError: the spaces at the start matter

Python uses the spaces at the start of a line to group code. A line indented for no reason stops the program.

price = 10
    print(price)  # error on purpose: this line shouldn't be indented
Python

Practice

Fix the indentation so the code prints 7.

days = 7
  print(days)
Python

4TypeError: the values don't fit together

A TypeError means you mixed values that can't work together, like adding text to a number.

print("Total: " + 5)  # error on purpose: you can't add text and a number
Python

Practice

Fix the code so it prints Total: 5 without an error.

print("Total:" + 5)
Python

Tip

Read error messages from the bottom up. The last line says what went wrong; the line number says where to look.

Quick check

Your code stops with NameError: name 'cutomers' is not defined. What's the most likely cause?