CODING
ADVENTURE
0 / 12 completed

FIELD NOTE 11 / BEGINNER PYTHON

Python debugging: understand errors and fix mistakes

Debugging means finding and correcting the reason a program behaves differently from your intention. An error message is evidence you can use.

The idea

Start with the smallest experiment that reproduces the surprise. Read the last line of the traceback, then look at the line number it names. Say what you expected and what happened instead. Change one thing, run again and compare. Keep a working version before a bigger experiment. This is the same habit as checking a clue rather than trusting your first theory.

A tiny experiment

PYTHON 3PREDICT BEFORE YOU RUN
clues = 2
print("Before:", clues)
clues = clues + 1
print("After:", clues)
assert clues == 3
Run and change this example ↗
Reveal the expected output
Before: 2
After: 3

Read it one step at a time

  1. The first print shows the value before the change. These temporary messages help you follow state.
  2. The second print checks the result of one step, not just the final outcome of a long program.
  3. assert checks a claim. A true condition is silent; a false one raises AssertionError. It is useful for experiments and tests.

Make one change

Change the increment to 2. Predict the prints and whether the assertion succeeds.

Check your thinking

It prints Before: 2 and After: 4, then raises AssertionError.

Keep following the trail

Sources & further reading

Companion notes for Coding Adventure by James Colby. Historical claims link to their sources; the examples are written for this learning trail.