Lessons and the learning game

Thinking like a programmer

The habits behind the code, right after Python from zero: break it down, spot patterns, plan, trace and debug, then build a sorting robot on a Raspberry Pi 4.

The course

Thinking like a programmer is a beginner Coding course in seven lessons. It comes right after Python from zero and teaches no new language features. Instead it teaches the thinking programmers do before and while they type, each habit practiced on a small, real program. Most lessons run on the gallery's two-LED Pi (red on GPIO17, yellow on GPIO27); flowcharts and the project use the traffic light module with a button. Every step is checked against what the Pi prints or what its lights do.

  1. Break it down: a rocket launch written as one long list, split into named parts, parts that take inputs so one function checks every system, a part that answers GO or NO GO, and lights for the verdict.
  2. Patterns and loops: the two questions behind every loop (what stays the same, what changes), a rocket shape drawn from a rule, 1 + 2 + ... + 100 by loop and by Gauss's pairing trick (5050 both ways), and a beacon pattern stored as data.
  3. Flowcharts to code: boxes in a row become lines in order, a diamond becomes an if, an arrow back becomes a while, and a wait for a person becomes a button, on a traffic light. Then you draw your own and code it.
  4. Pseudocode first: a day of hull temperatures planned in plain words and translated line by line into the hottest reading (31), the average (25.1) and the hot hours, checked against max() and sum(), with an alarm on the red LED.
  5. Tracing by hand: trace tables filled in before running, a trace printed to check them, a while loop followed to its last turn (7 days to burn 100 kg of fuel down below 1 kg), a swap bug found on paper, and nested function calls followed inside out.
  6. Debug a recipe: predict, run, compare, fix one thing. A pancake program with four bugs: a scaling ratio upside down, an off-by-one loop that drops the last step, two steps in the wrong order, and eggs that round to the even number instead of up.
  7. Project: a sorting robot on LEDs: the finale.

The project

A cargo robot sorts crates by weight using one move: compare two neighbors and swap them if the left one is heavier. The crates are drawn as bars on the Serial Monitor. One pass carries the heaviest crate to the end, repeated passes make a full bubble sort (16 comparisons and 6 swaps for five crates), and two improvements, shorter passes and stopping when nothing moves, cut it to 10. The traffic lights show the robot at work: amber for every comparison, red for every swap, green when the crates are in order. The last step counts the worst case, crates in reverse order, and finds the pattern behind it.

What the Pi engine does differently

The Pi runs MicroPython 1.29, not CPython. In this course that barely shows: the programs use lists, loops, functions, round, math.ceil and gpiozero, which behave as they do on a real Pi. The one thing to know is that a dict does not keep the order keys went in, so where order matters the lessons keep things in lists (the launch checks walk a list of system names, and the recipe is a list of pairs).

The gallery has no Raspberry Pi circuit with an LED bar yet, so the sorting robot shows its work on the traffic light module and draws the crates as bars of # on the monitor.