Review
Use the temperature report to recall the chapter's complete cycle: ask a question, predict the result, run and inspect the program, make one change, and repair one failure.
From a Question to a Program
For readings 18, 21, 20, and 23, the hand calculation is
The program performs the same calculation and displays selected results:
total: 82
count: 4
mean: 20.5
The source code contains Python instructions. The output contains the values
those instructions display. A calculated value does not become visible merely
because Python calculated it; this program uses print() to display it.
Predict, Run, and Change
The programs in this chapter have no choices or repeated steps, so read them from top to bottom. At each instruction, ask what Python now remembers and whether the instruction adds a line of output. Later chapters add choices and repeated steps.
Before changing the program, calculate the expected effect. Replacing the final reading 23 with 27 changes the total from 82 to 86 and the mean from 20.5 to 21.5. The count remains 4. Changing only one reading lets us connect one cause with the changed result.
Labelled intermediate output helps with that comparison. A comment can add context, such as the unit of measurement, but it does not run and does not appear in the output.
Read a Failed Run
If the division line uses totl instead of total, Python stops before the
report is printed. Read the traceback from its final line:
NameError: name 'totl' is not defined
Then find the source location that uses totl, compare it with the spelling
created earlier, and make one local repair. Run the program again to verify that
the expected report returns.
The important habit is not memorizing NameError. It is using the evidence in
the failed run: the source location, the name mentioned by the final line, and
the result of the repaired run.
The Complete Cycle
The chapter's method can be stated in seven moves:
- State a small question whose answer can be checked.
- Calculate or predict the result.
- Run the program.
- Separate source code, ordinary output, and an error report.
- Compare the actual result with the prediction.
- Change or repair one relevant part.
- Run again and check the complete report.
This method will remain useful when the programs contain more names, more data, and more than one possible path through their instructions.