A small program, end to end
Everything at once: a tiny to-do list that uses a function, a loop, a list and a file — and what to build next.
Everything, in one small program
A program worth keeping is rarely one clever line; it is a handful of ordinary pieces arranged clearly. The to-do list below uses only what this course has already taught: a list to hold the items, a function to add one, a loop to print them, and a file to keep them.
pass means "nothing here yet". It is the honest placeholder for a body you have not written, and it keeps the file runnable while you work.
The shape of the program
todos = ["buy flour", "feed the starter"]
def add(item):
pass # one job: add the item, and say so
# a loop to print them, a file to save themPlan in words first
Before typing, say what the program does in two or three plain sentences: keep a list of things to do; a function adds one and confirms it; print the list numbered; save it to a file and say so.
A plan in words costs a minute and saves ten. It also decides the shape — a list here, a function there — before the details start arguing with each other.
The to-do list, finished
Finish the program: a function that adds an item and confirms it, the list printed as numbered lines, and every item saved to todos.txt before a final line that says Saved.
Where to go next
You have met the whole shape of the language now: values and names, decisions, loops, functions, lists and dictionaries, files, errors, classes and modules. Nothing on that list is optional for real work, and all of it is now yours to practise.
The way to practise is to build something small that you actually want — a bill splitter, a quiz, a habit tracker. Read other people’s code when it confuses you, and reach for the official tutorial when a word is unfamiliar. The next program should be one you would miss if it stopped working.
The smallest version that runs
The first version of a program should do one visible thing. For the to-do list that is printing the two starting items; everything else — the function, the numbering, the file — is added to a program that already runs. A working skeleton is easier to extend than a complete program that has never executed.
This is not a preference for small ambitions. It is a way of keeping the number of possible causes small. When the skeleton runs and the next slice does not, the new lines are the only suspects.
A program is read far more often than it is run
The reader may be you in a month, with no memory of why a line is there. Names that say what they hold, functions that do one thing, and a short comment at each step are not decoration; they are how the program survives the interval.
The test is simple: can you explain each line out loud without guessing? A line you cannot explain is either unnecessary or in need of a better name. Deleting it is often the fastest improvement available. That explanation is also the comment worth writing — the reason, not the syntax.
The best next program is one you actually want to exist. Want is what makes you finish it, and finishing is what teaches.
Build it in runnable pieces
A program this size is easier to finish in slices than in one pass. Get the list printing first, then add the function, then the file. After each slice, run it: a small program that works tells you which slice broke, while a large one that fails tells you only that something did.
Print the state between slices — the list after adding an item, the text before writing it. The output is not clutter; it is the only view you have of what the program currently believes.
Name the data before the steps
Start by naming the data: a list of text for the to-dos, a dictionary for counts, a filename for the report. The shape decides which operations are easy — appending to a list, looking up a key, writing a line.
Then name the steps as verbs: add, print, save. Each verb is a candidate for a function or a short block. If a step needs the word “and”, split it.
Three capstone traps: writing every piece before running any of it, so the first error could be anywhere; defining a function after the line that calls it (definitions run top to bottom like everything else); and writing file lines without \n, so the saved list becomes one long line.
Try these next
- Write the numbered print loop first, before the function or the file, and run it.
- Add the function and print the list again; only then add the save step.
- Open the saved file and print it back, so the last check is the file’s contents rather than a claim.
A word count that saves a report
Finish the program: count how often each word appears, print the counts in alphabetical order, write the same lines to counts.txt, and print Saved when the file is closed.
Check what you learned
One question at a time. A wrong answer explains itself — take another go.
Part 1 of 5
Which structure suits "a word and its meaning"?
