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.

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.

LessonTimeWhat 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
  1. It crashed. Good.
  2. Follow it to the cause
  3. A NameError
  4. A KeyError
  5. Catch it, keep the traceback
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
  1. No crash, no lamp
  2. Print what it knows
  3. Fix it
  4. From print to logging
  5. 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.

LessonTimeWhat 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
  1. Below empty
  2. Make the program check itself
  3. Fix the bug, keep the check
  4. Asserts as tests
  5. Let a test find the next bug
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
  1. A very deep ocean
  2. Check the middle
  3. Fix stage 6
  4. Bisect the data
  5. 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.

LessonTimeWhat 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
  1. One past the end
  2. The average is wrong too
  3. The wrong side of the line
  4. Fence posts and slices
  5. Your off-by-one checklist
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
  1. A toggle that will not toggle
  2. Say which variable you mean
  3. The shared default
  4. The backup that empties itself
  5. 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.

LessonTimeWhat 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
  1. Script A: the beacon
  2. Fix the beacon
  3. Script B: the dive log
  4. Fix the log
  5. Script C: the mode button
  6. Fix the mode button
  7. 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.