Exercises
Practise hand prediction, source and output reading, controlled changes, useful comments, and one-name repairs using small runnable reports.
Read and Predict
Q1. Calculate before running
Four readings are 12, 16, 14, and 18. What is their mean?
Compute it first, then check your number.
HintFind the total first
Calculate 12 + 16 + 14 + 18, then divide that total by 4.
SolutionThe mean is 15
The total is 60 and the count is 4, so the mean is .
Q2. Identify source code
Which item is a Python instruction rather than output?
Select one choice, then check.
HintLook for an instruction
Source code may contain a function name and parentheses. Output contains only what the running program displayed.
SolutionThe print call is source code
print("mean:", mean) is a Python instruction. mean: 20.5 and 20.5 can
appear as output.
Q3. Predict every output line
What does this program display?
Select one choice, then check.
HintCount the print calls
The program calculates three values but contains only two print() calls.
SolutionThe report has two lines
The total is 21 and the mean is 7.0. The program prints total: 21 followed
by mean: 7.0; it does not print the count.
Q4. Find the first visible result
Which instruction first adds a line to this program's output?
Select one choice, then check.
HintSeparate calculation from display
Look for the first instruction whose task is to display a value.
SolutionThe first print call adds output
print("total:", total) displays the first line. The three preceding
instructions calculate and remember values without displaying them.
Change and Inspect
Q5. Predict one changed reading
The readings are 10, 12, 14, and 16. If only the final reading changes from 16 to 20, what is the new mean?
Compute it first, then check your number.
HintKeep the count fixed
There are still four readings. Only the total changes.
SolutionThe new mean is 14
, and .
Q6. Choose a useful comment
Which comment adds information that the instruction itself does not show?
Select one choice, then check.
HintRemove repeated wording
Two choices merely describe source code that is already easy to read.
SolutionRecord the unit
# Readings are measured in degrees Celsius. adds information absent from
the calculation. The other comments repeat visible instructions.
Repair and Rebuild
Q7. Use an error to repair one name
The program should display a total of 42, a count of 3, and a mean of 14.0. Run it, read the error, repair one misspelled name, and run it again.
Editable Python
Ready to run.
HintRead the final error line
Find the quoted name Python could not locate, then compare its spelling with
total on the first line.
SolutionReplace toatl with total
Use mean = total / count. The program can then divide 42 by 3 and display
the complete report.
Q8. Complete a small rainfall report
The rainfall measurements are 3, 5, and 4 millimetres. Edit the first three lines so that the program displays a total of 12, a count of 3, and a mean of 4.0. Keep the labelled print instructions.
Editable Python
Ready to run.
HintFollow the temperature program's shape
Add the three measurements on the first line, remember 3 on the second, and
calculate the mean from total / count on the third.
SolutionUse the three rainfall measurements
The first three lines are total = 3 + 5 + 4, count = 3, and
mean = total / count. They produce 12, 3, and 4.0.