Debug a Wrong Answer
Check one plausible numerical result against a hand-worked case, compare expected and actual loop state, and repair the first divergence rather than the final number.
A program can run to completion and still return the wrong answer. Without a traceback, the first evidence must come from an expectation worked out independently.
Suppose the earlier text-value defect has been repaired and the available readings have been extracted from the sensor records. The calculation now uses this small list:
readings = [18.0, None, 24.0]
None means that no numerical reading is available at that position. The mean
therefore uses two readings:
expected total = 18.0 + 24.0 = 42.0
expected count = 2
expected mean = 42.0 / 2 = 21.0
Suppose the program instead prints 14.0. That result is plausible enough to
look like a real measurement, but it disagrees with the hand calculation.
Compare Expected and Actual Results
Here is the faulty function:
The function does not crash. It returns:
Record the disagreement before editing:
| Quantity | Expected | Actual |
|---|---|---|
| total | 42.0 | 42.0 |
| count | 2 | 3 |
| mean | 21.0 | 14.0 |
The total agrees. The count does not. This narrows the investigation to the
rule for updating count; changing division or rounding would move away from
the evidence.
Q1. Work out the expected mean
What numerical mean should [18.0, None, 24.0] produce under the established
rule that None is skipped?
Compute it first, then check your number.
HintCalculate independently
Add 18.0 and 24.0, then divide by the number of available readings.
SolutionThe expected mean is twenty-one
The two available readings total 42.0. Dividing by 2 gives the independently known result 21.0.
Find the First Divergence
Final values show which state is wrong. A loop trace shows when it first becomes wrong. Compare the intended and actual state after each input:
| Reading examined | Expected total | Actual total | Expected count | Actual count |
|---|---|---|---|---|
| starting state | 0 | 0 | 0 | 0 |
18.0 | 18.0 | 18.0 | 1 | 1 |
None | 18.0 | 18.0 | 1 | 2 |
24.0 | 42.0 | 42.0 | 2 | 3 |
The first divergence occurs on the None iteration. The expected count remains
1 because no numerical reading was processed. The actual count becomes 2
before the condition skips the unavailable value.
That first difference points directly to the misplaced update:
Move the count update after the skip:
Now count changes only on the same iterations that add a numerical reading to
total.
Q2. Locate the first wrong state
When does the faulty function first disagree with the intended trace?
Select one choice, then check.
HintDo not begin at the final mean
The starting state and the 18.0 iteration agree in both traces.
SolutionThe count diverges at None
After None, both totals remain 18.0, but the expected count is 1 and the
actual count is 2. This is the first broken state.
Check Initialization, Updates, and Boundaries
Wrong-answer debugging benefits from a fixed set of questions:
- Does each state name start with the value required before the first item?
- Does every input get examined once?
- Does each update run only for inputs that should affect it?
- Does the final calculation use state with the intended meaning?
For this function, total = 0 and count = 0 are correct initial values. The
loop examines all three entries once. The broken rule is narrower: count
updates before the function decides whether the current reading is available.
The repaired function is:
Verify both the original failing case and nearby boundaries:
The first check prevents this count-placement defect from returning unnoticed. The other checks preserve the function's established behavior.
Q3. Repair the misplaced count update
Move the count update so the program displays 21.0. Keep the existing
None return for inputs with no available readings.
Editable Python
Ready to run.
HintKeep related updates on the same path
Move count = count + 1 below the continue, beside the total update.
SolutionCount only available readings
A wrong answer needs an independently known expectation. Compare expected and actual state after each step, stop at the first divergence, and repair the rule that produced it. Regression checks preserve the original case and its boundaries. The next lesson will distinguish a legitimate absent result from invalid input that a function must reject clearly.