Exercises

Locate failures, trace bad state, repair a wrong mean, choose among four policies, validate and assert precisely, narrow a handler, and keep a regression case.

Locate and Trace the Problem

Q1. Choose the first useful traceback frame

A program calls parse_reading, which fails while converting "missing". The browser runner adds two internal frames above the reader's code. Where should the investigation begin after reading the final exception line?

Choose one

Select one choice, then check.

HintSeparate environment code from reader code

Find the last frame that points into the program being investigated.

SolutionBegin where the conversion stopped

Read the final exception line, then inspect the deepest relevant frame in parse_reading. Earlier runner frames usually describe how that function was reached.

Not attempted
Review

Not marked done.

Q2. Separate the failure location from its cause

The expression total += reading stops because reading contains the text "18.5". Which statement is accurate?

Choose one

Select one choice, then check.

HintAsk two questions

Where could Python no longer continue? Where did reading receive its text value?

SolutionTrace the value backward

The mixed addition is where Python stops. The missing or incorrect conversion can occur earlier, where external text first enters the numerical path.

Not attempted
Review

Not marked done.

Q3. Find the first bad state

This trace comes from a loop that should convert each text reading before adding it:

PositionIncoming valueConverted valueTotal afterward
0"18.5"18.518.5
1"20.0"20.038.5
2"missing"

Which state should be investigated first?

Choose one

Select one choice, then check.

HintRead from the first divergence

The earlier rows establish what successful conversion looks like.

SolutionInspect position 2

"missing" is the first incoming value that cannot become a number. The absent total is a later consequence.

Not attempted
Review

Not marked done.

Repair a Wrong Answer and Choose a Policy

Q4. Repair the available-reading mean

The program should ignore None and print 21.0. Repair the count and denominator without replacing the calculation with a fixed answer.

Editable Python

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

Ready to run.

HintCompare the two counts

The list has three positions but only two available readings.

SolutionUpdate and use the available count
Not attempted
Review

Not marked done.

Q5. Choose the policy from the situation

Which mapping uses the four chapter policies correctly?

Choose one

Select one choice, then check.

HintName who owns the rule

Caller input is validated; internal state is asserted; expected conversion failure is handled only when a response is defined.

SolutionKeep the decisions distinct

The first mapping is correct. These mechanisms are not interchangeable even when each could make the immediate traceback disappear.

Not attempted
Review

Not marked done.

Q6. Reject reversed thresholds

Complete the boundary check. The program must print the stated message for the invalid call and ordinary for the valid call.

Editable Python

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

Ready to run.

HintValidate the relationship

Equality is allowed. Reject only the case in which the lower threshold is greater than the upper threshold.

SolutionRaise near the boundary
Not attempted
Review

Not marked done.

Assert, Handle, and Verify

Q7. Place the internal invariant

Add an assertion immediately after processed changes. It must state that the processed count cannot exceed the number examined.

Editable Python

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

Ready to run.

HintCheck immediately after the update

At that point both counters describe the prefix already visited.

SolutionState the rule and observed counts
Not attempted
Review

Not marked done.

Q8. Handle only invalid numeric text

Complete the narrow handler. Valid readings must be added, invalid numeric text must be reported and skipped, and the program must print the final total.

Editable Python

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

Ready to run.

HintKeep the try block narrow

Only float(text) is expected to raise the conversion failure handled here.

SolutionCatch the specific boundary failure
Not attempted
Review

Not marked done.

Q9. Keep only a valid reduction

The original program fails while converting the third row's reading from "missing" to a number. Which reduction preserves the same failure?

Choose one

Select one choice, then check.

HintPreserve cause and operation

Remove unrelated rows, not the invalid value or the conversion that exposes it.

SolutionKeep the corrupted row

The one-row case preserves both "missing" and the numeric conversion. A different exception or a successful run would not be the same failure.

Not attempted
Review

Not marked done.

Q10. Retain the regression case

Repair parse_readings so it reports and skips invalid numeric text. Keep the original corrupt record as a regression case, then rerun the full dataset.

Editable Python

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

Ready to run.

HintRepair only the expected boundary

Catch ValueError from float(text), report the text, and continue before the append.

SolutionKeep the case that once failed

The first assertion keeps the original bad input. The second protects the ordinary successful path and confirms that one invalid record does not remove valid readings.

Not attempted
Review

Not marked done.

Pause and reflect

Which exercises were difficult, what mistake pattern did you notice, and what should you practice again? The note stays with this exercise set.

0 of 10 exercises marked done

Review

Not marked done.