Exercises
Trace names, predict numerical expressions, choose readable identifiers, build labelled reports, and repair one text-and-number mismatch.
Trace and Predict
Q1. Trace names and output
What does this program display?
Select one choice, then check.
HintSeparate remembered values from output
The first three lines change names but do not display anything.
SolutionThe current values are 21 and 41
The second line reassigns reading to 21. total then becomes 41, and the
two print calls display those values in order.
Q2. Predict a numerical result
Evaluate (23 - 20) ** 2 + 10 // 4 + 10 % 4 before running it.
Compute it first, then check your number.
HintCalculate three smaller results
Find (23 - 20) ** 2, 10 // 4, and 10 % 4 separately.
SolutionThe result is 13
The three parts are 9, 2, and 2. Their sum is 13.
Q3. Choose a readable name
Which valid name best describes a temperature's distance from the chosen reference value?
Select one choice, then check.
HintCheck both rules and meaning
A name cannot begin with a digit, and a one-letter name hides the value's role here.
SolutionUse distance_from_reference
The name follows ordinary Python style and remains understandable without a separate comment.
Build and Repair Reports
Q4. Complete a labelled report
Complete the final line so the program displays Mean: 20.5 °C.
Editable Python
Ready to run.
HintUse one decimal place
Put {mean:.1f} and {unit} inside an f-string.
SolutionFormat the mean inside the report
print(f"Mean: {mean:.1f} {unit}")
Q5. Repair a numerical input
The count arrived as text. Repair the program so it displays mean: 20.5.
Editable Python
Ready to run.
HintInspect the count's meaning
The characters "4" genuinely represent a whole-number count, so int()
is appropriate.
SolutionConvert the count once
count = int(count_text)
Q6. Construct a rainfall report
The rainfall readings are 3, 5, 4, and 8 millimetres. Replace the starter
values and final report so the program displays Mean rainfall: 5.0 mm.
Editable Python
Ready to run.
HintKeep calculation and report roles distinct
Use 3, 5, 4, and 8 as the reading values. Then display mean with
{mean:.1f} inside an f-string.