Change One Thing and Run Again

Change one temperature reading, predict the new total and mean, and compare the rerun with that prediction. Use labelled intermediate output and one useful comment to keep the change understandable.

A program becomes more useful when we can change it and explain the new result. Changing one part at a time keeps that explanation manageable: we know which change could have caused the difference we observe.

Predict the Change First

Return to the four readings from the first program:

18, 21, 20, 23

Suppose the final reading should be 27 rather than 23. Before editing the program, calculate what should change:

18+21+20+27=86,18 + 21 + 20 + 27 = 86,

and

864=21.5.\frac{86}{4} = 21.5.

The count remains 4 because we changed a reading rather than adding one.

Now edit only 23 in the first line, replacing it with 27. Run the program and compare all three lines with the prediction.

Change one temperature reading

Replace 23 with 27. The expected total is 86, the count remains 4, and the expected mean is 21.5.

Command/Ctrl + Enter. Python runs in your browser.

Ready to run.

The useful cycle is short:

  1. predict the current result;
  2. run the program and inspect it;
  3. change one relevant part;
  4. predict the changed result;
  5. run again and compare.

A wrong prediction is still useful. The difference tells us that either our hand calculation or our reading of the program needs another look.

Keep Intermediate Results Visible

The program displays total and count as well as mean. Those intermediate results help us locate a disagreement.

If the program prints

total: 86
count: 4
mean: 21.5

then every displayed value agrees with our hand calculation. If the total were 86 but the mean were not 21.5, the division line would deserve attention. If the total itself were wrong, we would inspect the readings in the first line.

Printing every value in a large program would create noise. Here, three short labelled lines make the calculation easier to inspect.

Use a Comment When It Adds Context

The numbers alone do not tell a reader which unit they use. A Python comment can record that context:

Outside quoted text, # begins a comment. Python ignores the rest of that line when it runs the program. The comment is present for a person reading the source, so it does not appear in the output.

A useful comment adds information that the code does not already state. This comment is less useful:

It repeats the instruction without explaining why four is the correct count.

Q1. Make one controlled change

Change only the final temperature reading so that the program displays total: 86 and mean: 21.5. Keep the count and all three print instructions unchanged.

Editable Python

Command/Ctrl + Enter. Python runs in your browser.

Ready to run.

HintThe count does not change

Keep four readings and replace the last value. The hand calculation above shows which replacement produces a total of 86.

SolutionReplace 23 with 27

The first line becomes total = 18 + 21 + 20 + 27. The total is 86, the count remains 4, and the mean is 21.5.

Not attempted
Review

Not marked done.

A controlled change begins with a prediction and ends with a comparison. Labelled intermediate output helps us explain the result, while a useful comment records context the instructions cannot show. The next lesson keeps the same program but introduces one change that prevents it from running.

Pause and reflect

In your own words, note what you understood, what remains unclear, or what you want to revisit. The note stays with this lesson.

0 of 1 exercises marked done

Review

Not marked done.