HomeCourseModule 02 › When things break — reading errors without panic

When things break — reading errors without panic

Module 02 · Setting Up Your Environment8 min readBeginner

What you'll learn

  • Read a Python traceback bottom-up
  • Recognise the five errors you'll see most
  • Know the standard fix for each

The good news about Python errors

Python errors look intimidating because they're long. They're actually friendly: the last line tells you exactly what went wrong, and the line above that tells you where. Read bottom-up and ignore the rest until you're more advanced.

Anatomy of a traceback

Traceback (most recent call last):
  File "report.py", line 12, in <module>
    total = price * quantitiy
NameError: name 'quantitiy' is not defined

Read bottom-up:

The five errors you'll see most

1. NameError: name 'x' is not defined

Meaning: Python doesn't know what x is.
Usual cause: Typo, or you forgot to create the variable, or you tried to use it before the line that defines it.

Fix: check spelling and make sure the variable is created on an earlier line.

2. SyntaxError: invalid syntax

Meaning: The code doesn't follow Python's grammar.
Usual cause: Missing colon at the end of an if/for/def line. Or a bracket that wasn't closed. Or a quote that wasn't closed.

# Wrong: no colon
if age > 18
    print("adult")

# Right
if age > 18:
    print("adult")

3. IndentationError

Meaning: Wrong amount of leading whitespace.
Usual cause: You used a tab where Python expected spaces, or you indented inconsistently.

Fix: pick four-space indentation everywhere. VS Code does this for you if you stay on the Python file mode.

4. TypeError

Meaning: You're using a value of the wrong type.
Usual cause: Trying to add a number and a string.

# Wrong: "5" is a string, not a number
total = "5" + 10

# Right
total = int("5") + 10   # 15

5. FileNotFoundError

Meaning: The file you asked Python to open doesn't exist at that path.
Usual cause: Typo in the filename, wrong folder, or the file extension is wrong.

Fix: print the current folder first and double-check:

import os
print(os.getcwd())     # which folder am I in?
print(os.listdir())    # what's in this folder?

The "first response" checklist

  1. Read the last line of the error.
  2. Look at the line number just above it.
  3. Re-read that line in your code, slowly.
  4. If still stuck, paste the error into Google or an AI assistant verbatim.
  5. Rerun, see if the error changes. A different error = progress.
💡 Pro tip
Don't try to "guess" what's broken. Always re-read the actual error message. 95% of the time it tells you exactly what to fix.

Key takeaways

  • Read tracebacks bottom-up; the last line is the actual error.
  • The five common errors: NameError, SyntaxError, IndentationError, TypeError, FileNotFoundError.
  • Each has a one-line fix. You'll memorise them after seeing each three times.
  • Copy-paste the error into search; you're never the first to hit it.

Make these errors happen on purpose

In a Jupyter cell, run each of these and see what error you get. Then fix it.

# 1
print(secret_number)

# 2
if 5 > 3
    print("yes")

# 3
"hello" + 5

# 4
open("not_a_real_file.txt")

Reading errors gets easier the more of them you see. Welcome to programming.

📹 Video walkthrough
A video walkthrough of this lesson will be embedded here. Until then, the written walkthrough above mirrors what the video will cover step-for-step.