How to write lessons that teach quickly and check reliably.
content/<NN-module>/<NN-lesson>/
├── lesson.md
├── starter.py
├── solution.py
└── test_exercise.py
The quickest way to start is python tools/new_lesson.py "Title" --module <module>, which creates these files with the numbering and prerequisite filled in (see CONTRIBUTING.md).
Folder names are NN-lowercase-words (for example 03-data-structures/02-dictionaries). The numbers set the order. A lesson id is <module>/<lesson>, for example 01-foundations/02-variables.
---
title: Variables # shown to learners
minutes: 12 # honest estimate, 1-60
prereqs: ["01-foundations/01-hello-world"] # lesson ids that must come earlier; [] for none
concepts: [variable, assignment] # what this lesson teaches (used later for review and placement)
local_only: false # true if it cannot run in a browser (see below)
hints: # exactly three, each a little more revealing than the last
- "..."
- "..."
- "..."
---Quote any frontmatter value that contains :, * or # (for example concepts: ["*args"]); the validator reports invalid YAML with a hint.
Optional keys:
broken_starter: trueallows astarter.pythat does not even parse (only for lessons about fixing errors).packages: [numpy, pandas]lists the third-party packages the lesson needs. Onlynumpy,pandasandmatplotlibare allowed. In the browser, Pyodide downloads them before the lesson starts (the first time takes a while, and the page says so). Locally they come fromrequirements-tracks.txt. A lesson whose output cannot be seen in the browser (for example a saved chart) should also belocal_only: true.
Unknown keys are rejected.
A module whose module.json contains "optional": true is an optional track (Data Analysis, Automation & scripting, Web & APIs). Tracks are listed after the core course, are never counted in the core progress, and are left out of the placement test and the fast track. A core lesson may not list a track lesson in prereqs; a track lesson may build on core lessons. Give each track lesson review questions like any other lesson. Keep project lessons at the end of a track and title them Project: ....
Use these headings in this order (## Your turn and ## Recap are required; CI checks):
## Why this matters: one to three sentences.## The idea: at most about 150 words and one worked example.## Try it: invite the learner to change the example and predict the result.## Your turn: exactly what to do and what "done" looks like.## Recap: three bullets at most.## Common mistakes: the real errors learners hit, with the error name they will see.
- Friendly, direct, second person ("you"). No jokes at the learner's expense.
- Introduce one idea at a time. If a line of starter code uses something not taught yet (like
int(input())), say so and tell the learner to leave it alone. - Show errors as normal. Name the exact error type the learner will see.
- Plain words first, jargon second (bold the term once, when it is defined).
- Prefer a task that takes 2 to 5 minutes and has one clear success condition.
- The starter must fail the tests, and the solution must pass.
python tools/check_solutions.pyproves both. - Use comments in
starter.py(# TODO 1: ...) to break the task into steps. - Do not require a specific coding style when behaviour is what matters.
Tests are ordinary pytest tests, run by real pytest locally and by a small compatible runner in the browser.
Rules:
- Every
asserthas a message that tells the learner what to do:assert out == "Hello, Python!\n", f"Expected Hello, Python! but your program printed {out!r}."
- Import the learner's code inside the test:
import exercise. Each test gets a fresh import, which runs the learner's program. Do not import at the top of the file. - Give each behaviour its own test function with a descriptive name (
test_exactly_25_is_hot). The learner sees these names. - Provide feedback a beginner can act on: show what was expected and what they got, using
!rfor strings.
| Supported | Notes |
|---|---|
test_* functions, assert |
|
capsys fixture |
capsys.readouterr() with .out / .err |
monkeypatch fixture |
setattr("module.attr", value), setattr(obj, "name", value), setitem, chdir(path); undone after each test |
tmp_path fixture |
a fresh empty temporary folder (a pathlib.Path), removed after the test. Write file lessons as functions that take a path so the checks never touch the learner's own files |
pytest.raises, pytest.approx, pytest.fail |
approx handles numbers only |
Anything else (parametrize, request, plugins, ...) works locally but not in the browser. If a lesson needs it, either rewrite the test to avoid it or mark the lesson local_only: true.
Patch it in the test with monkeypatch.setattr("builtins.input", lambda prompt="": "30"). In the browser, learners type values into the Input box when they press Run.
- Randomness: make the function take a
random.Random(for exampleroll_dice(count, rng)), and have the test passrandom.Random(42). Never assert on unseeded random output. - Files: use
tmp_pathfor anything the learner's code writes. If the code works relative to the current folder, callmonkeypatch.chdir(tmp_path)so a failing test cannot leave files behind. - Logging: give each test a fresh logger name, because loggers are global and would otherwise carry handlers from one test into the next.
- Collaborators: when a lesson is about composition, test with a small fake object (a duck type) instead of the learner's other class.
- Report text: when a result contains values that may legitimately be formatted in more than one way, check the pieces (for example
"12.50" in line) instead of the exact whole line.
Browsers cannot provide real network sockets, subprocesses or threads. Lessons about those topics set local_only: true. When a lesson needs a server, start one inside the test on 127.0.0.1 (for example with http.server) rather than calling a real website, so the checks never depend on the internet or on someone else's service. They appear on the website with instructions to use the local track, and the browser-runner tests skip them. Their tests may use any pytest feature. In practice that means parametrize, fixtures, skip, asyncio.run, Git, packaging tools and anything else the in-browser pytest cannot do.
- Tests-of-tests: to teach testing, have the learner write the tests. The checker runs them against a correct reference and against deliberate mutants, and the tests must fail on every mutant (see
10-testing-and-quality/01and the capstone). - Checking code style in the browser: use
astpluslinecache.getlines(exercise.__file__). The runner registers the learner's source there, so this works in the browser and locally. - Each test imports
exercisefresh: the runner removes it fromsys.modulesbetween tests, so do not rely on state surviving.
-
python tools/validate_content.pyis clean -
python tools/build_exercises.pyhas been run and its output committed -
python tools/check_solutions.pypasses -
cd web && npm testpasses - A new lesson has one or two questions in
content/review/questions.json - You read the lesson in the browser (
npm run dev) and it makes sense to a complete beginner
The placement test lives in content/placement/questions.json. Each question is a short program and four or so options.
- Keep questions in course order (by the lesson they test). Questions for topics with no lesson yet use
"lesson": nulland a"topic", and go last. - Options are plain strings; use
\nfor options that print several lines. Add"stdin"if the program reads input. Use"kind": "error"when the question is "which error is this?", and list the exception name as the right option. - Every question needs an
"explanation"that teaches, in one or two sentences. - You do not need to work out the answer by hand:
python tools/validate_content.pyruns each program and fails if the answer key does not match what Python really prints.
Each lesson (except projects and the orientation lesson) needs one or two questions in content/review/questions.json. They feed the spaced-review queue, so aim for retrieval of the key idea, not trivia:
- Same format as the placement questions, but every question needs a
lesson; keep the file in course order. - Prefer short programs whose output shows the idea (and the usual misconception as a wrong option). Use
"kind": "error"for "which error is this?". - You do not work out the answer by hand:
python tools/validate_content.pyruns the code and fails if the key is wrong. CI also fails if a lesson has no review question.