Source: https://mokxi.com/teachers/plans/debugging-python
Updated: 2026-11-19

Lesson plan

# Debugging Python lesson plan

Find and fix real bugs in a deep sea probe's Python on a Raspberry Pi: tracebacks, print and logging, asserts, bisection, off-by-one errors and state bugs, then three broken scripts as the boss.

4 chapters, 7 lessons, about 5 hours, on the Raspberry Pi 4 Model B. Every lesson runs in the browser, with nothing to install.

Start a class from this course

The unit

## Objectives and materials

### By the end, students can

- Tracebacks, prints and logs: make the program tell you what went wrong.
- Asserts that catch a bug where it starts, and halving that finds it anywhere.
- Off by one and remembered state: the two bugs every program meets.
- Three broken scripts, one hour to the dive.

### Materials for the whole unit

- Half breadboard
- LED x 2
- Motion sensor
- Pushbutton
- Raspberry Pi 4 Model B
- Resistor x 2
- Traffic light module

Every part is already on the simulator’s bench, so there is nothing to buy or hand out. The counts are the most any one lesson uses, if you build it on real breadboards too.

Chapter 1. About 1 h 20 min

## Read the clues

Tracebacks, prints and logs: make the program tell you what went wrong.

Lesson | Time | What students do

Reading a traceback The Nereus depth script crashes. Read a MicroPython traceback from the bottom up, fix a bad reading, a misspelled name and a missing key, and catch bad input without losing the traceback. | 40 min

- It crashed. Good.
- Follow it to the cause
- A NameError
- A KeyError
- Catch it, keep the traceback

Print vs logging The sonar contact lamp never lights. Find out why with print statements, fix it, then turn the prints into logging with levels, names and line numbers you can switch off without deleting a line. | 40 min

- No crash, no lamp
- Print what it knows
- Fix it
- From print to logging
- Turn the volume down

### Materials

- Half breadboard
- LED x 2
- Motion sensor
- Raspberry Pi 4 Model B
- Resistor x 2

### Check for understanding

Each student finishes "Print vs logging" on their own, without opening the hints. The simulator checks 5 of its 5 steps as they go.

Then ask each one to explain, in a sentence or two, the last thing it asked of them: "Turn the volume down".

Chapter 2. About 1 h 20 min

## Set traps

Asserts that catch a bug where it starts, and halving that finds it anywhere.

Lesson | Time | What students do

Asserts The ballast tank goes below empty without a word. Make the program check its own assumptions with assert, fix the bug the assert catches, and turn asserts into small tests that guard the fix. | 40 min

- Below empty
- Make the program check itself
- Fix the bug, keep the check
- Asserts as tests
- Let a test find the next bug

Bisecting a bug The sonar puts the sea floor at 21,579 m. Find the broken stage by halving the pipeline, then use the same halving to find one bad ping in a thousand and the first bad firmware version in sixteen. | 40 min

- A very deep ocean
- Check the middle
- Fix stage 6
- Bisect the data
- Bisect the history

### Materials

- Half breadboard
- LED x 2
- Pushbutton
- Raspberry Pi 4 Model B
- Resistor x 2

### Check for understanding

Each student finishes "Bisecting a bug" on their own, without opening the hints. The simulator checks 5 of its 5 steps as they go.

Then ask each one to explain, in a sentence or two, the last thing it asked of them: "Bisect the history".

Chapter 3. About 1 h 20 min

## The classics

Off by one and remembered state: the two bugs every program meets.

Lesson | Time | What students do

Off by one The most common bug in programming, four ways: an index one past the end, a loop that skips the first reading, a zone boundary on the wrong side, and a fence post. Each found with the same checklist and lit on the traffic lights. | 40 min

- One past the end
- The average is wrong too
- The wrong side of the line
- Fence posts and slices
- Your off-by-one checklist

State bugs Bugs that live in remembered values: a toggle that crashes on its first press, a default list shared by every dive, a backup that empties itself, and a deepest-point record that leaks from one dive into the next. | 40 min

- A toggle that will not toggle
- Say which variable you mean
- The shared default
- The backup that empties itself
- State that leaks between runs

### Materials

- Half breadboard
- LED
- Pushbutton
- Raspberry Pi 4 Model B
- Resistor
- Traffic light module

### Check for understanding

Each student finishes "State bugs" on their own, without opening the hints. The simulator checks 5 of its 5 steps as they go.

Then ask each one to explain, in a sentence or two, the last thing it asked of them: "State that leaks between runs".

Chapter 4. About 55 min

## Boss

Three broken scripts, one hour to the dive.

Lesson | Time | What students do

Boss: three broken scripts The boss: three of Nereus's scripts fail before a dive, a beacon that never flashes, a log summary that crashes on its first line and a mode button that dies on the third press. Find each bug with the right technique, fix it, and write the bug report. | 55 min

- Script A: the beacon
- Fix the beacon
- Script B: the dive log
- Fix the log
- Script C: the mode button
- Fix the mode button
- Write the bug report

### Materials

- Pushbutton
- Raspberry Pi 4 Model B
- Traffic light module

### Check for understanding

Each student finishes "Boss: three broken scripts" on their own, without opening the hints. The simulator checks 7 of its 7 steps as they go.

Then ask each one to explain, in a sentence or two, the last thing it asked of them: "Write the bug report".

Your class

## Teach this unit as a class

Start a class from this course and its 7 lessons are assigned in one go, in order. Students join with a six-character code or printed login cards, and each one’s work comes back to you.

The class tools are free for 30 days for one class of up to 20 students, with no card. The lessons themselves are free either way.

Start a class from this course See the demo class

More units

## Other lesson plans

Electronics basics 20 lessons, about 10 h. Embedded on the Arduino Uno 16 lessons, about 12 h. Embedded on the ESP32-C3 13 lessons, about 11 h. Embedded on the Raspberry Pi Pico 7 lessons, about 6 h. Build your own fight stick 5 lessons, about 4 h. Embedded on the STM32F411 6 lessons, about 6 h. C++ on a board 13 lessons, about 10 h. Digital logic and memory 4 lessons, about 4 h. Interactive devices on the Uno 9 lessons, about 8 h. From gates to a computer 23 lessons, about 16 h. Build a claw machine 8 lessons, about 6 h. Every unit All the courses as ready-made units, on the teachers page.
