Problem-solving recipes
A step-by-step procedure for every problem type this course teaches — what to do first, what to do next, and how to tell which case you're in.
What are you being asked to do?
What's in front of you right now?
- The program stopped and printed a report → Read a traceback. It runs but gives the wrong answer → Find a bug systematically.
- A task says "each", "every", "how many", or "until" → Write a loop to solve a problem.
- The same few lines appear twice, or one block does three jobs → Turn repeated code into a function. You want the function right the first time → Build a function test-first.
- Text typed by a person or read from a file has to match something → Normalize text before comparing it.
- A file of records needs summarising or converting → Write a program that processes a data file.
- You're starting a new project, or it needs a package from PyPI → Set up a Python project with uv.
- The package works and other people should be able to install it → Build and publish a library.
When something goes wrong
Read a traceback
Use when: a Python program stopped and printed a report
Goal: the line to fix and what is wrong with it
Input, output, and first errors
From Chapter 1. Start at the last line, then the frame just above it.
Find a bug systematically
Use when: a program raises an exception you don't understand, or finishes with a wrong answer
Goal: the cause found and fixed, and a check in place so it can't return
Debugging
From Chapter 9. Reproduce, narrow down, look, fix, and keep a test.
Writing the code
Write a loop to solve a problem
Use when: a task says each, every, how many, until, the first or the largest
Goal: a loop you have traced by hand and tested on its boundary cases
Solving problems with loops
From Chapter 4. Decompose, write the skeleton, trace it by hand, test the edges.
Normalize text before comparing it
Use when: text typed by a person, or read from a file, has to be compared, searched, or counted
Goal: a cleaned copy that treats spacing, accents and case as unimportant, with the original kept for display
Text-processing patterns
From Chapter 3. Strip, casefold, and collapse spaces before you compare.
Turn repeated code into a function
Use when: the same few lines appear in two places, or one block of code does a job you can name
Goal: one small, named, documented, checked function used everywhere the code was
Designing functions
From Chapter 6. Name the job, find its inputs and its result, then replace the copies.
Build a function test-first
Use when: you are about to write a function whose behaviour you can describe with examples
Goal: a working function where every behaviour has a test that was seen to fail first
Fixtures, edge cases, and TDD
From Chapter 9. Red, green, refactor, one small behaviour at a time.
Programs that work with data and projects
Write a program that processes a data file
Use when: a program must turn records in a file into a summary or a cleaned copy
Goal: a short program of one function per stage that reports bad records instead of crashing
Data-processing programs
From Chapter 8. Keep reading, checking, changing, and writing as separate steps.
Set up a Python project with uv
Use when: you are starting a new program or lab that will use any package from PyPI, or that you want to keep
Goal: a project folder whose pyproject.toml and uv.lock let anyone rebuild exactly your environment with one command
Projects with pyproject.toml and uv
From Chapter 7. A project folder, a pyproject.toml, an environment, and a lockfile you commit.
Shipping it
Build and publish a library
Use when: a package is ready for other people to install with pip or uv
Goal: a release on PyPI, uploaded by CI with no stored token, that installs and runs in a fresh environment
Building and publishing distributions
From Chapter 18. Finish the metadata, build, check in a fresh environment, then let CI upload with trusted publishing.