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.
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.
Read the clues
Tracebacks, prints and logs: make the program tell you what went wrong.
| Lesson | Time | What students do |
|---|---|---|
| Reading a tracebackThe 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 |
|
| Print vs loggingThe 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 |
|
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".
Set traps
Asserts that catch a bug where it starts, and halving that finds it anywhere.
| Lesson | Time | What students do |
|---|---|---|
| AssertsThe 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 |
|
| Bisecting a bugThe 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 |
|
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".
The classics
Off by one and remembered state: the two bugs every program meets.
| Lesson | Time | What students do |
|---|---|---|
| Off by oneThe 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 |
|
| State bugsBugs 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 |
|
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".
Boss
Three broken scripts, one hour to the dive.
| Lesson | Time | What students do |
|---|---|---|
| Boss: three broken scriptsThe 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 |
|
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".
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.