Exercises
- Predict and explain
"12" + "8"versus12 + 8. - Trace totals for
[12, 8, 12], then explain the empty-list case. - Write a pure
count_at_leastfunction. State its input assumptions. - Explain why replacing a list with a set changes a session tracker.
- Add boundary tests at 1 and 240 and rejection tests at 0 and 241.
- Demonstrate that reading corrupt storage leaves its contents unchanged.
- Add one meaningful test absent from the supplied checker and explain what defect it would catch.
- Complete the independent reading-page transfer task in the quiz.
Attempt before opening the answer key. Keep unsuccessful attempts and repairs as private learning evidence; publish only a deliberate project write-up.
Optional focused practice between lessons
Choose these when a concept is still uncertain; they are not additional exit gates. Allow roughly 15–25 minutes per drill, including prediction and checking. If you do all six beyond the scheduled practice, reserve an extra 1.5–2.5 hours and record the actual time. Attempt the relevant drill before using its answer explanation.
A. Values and text
Predict the displayed values without running:
minutes = 20
saved = minutes
minutes = minutes + 5
print(saved, minutes)
print("20" * 2)
print(20 * 2)
Then write a program that reads a whole number of positive minutes and displays complete hours and remaining minutes. Test 59, 60 and 61. State what conversion alone does not validate.
B. Branches and state
For the branch example in Lesson 2, classify 0, 1, 24, 25, 240 and 241. Trace a total for [5, 10, 5]. Explain what changes if the total is initialized inside the loop. Predict the countdown output when remaining starts at 1 and at 0.
C. Function boundaries
Write minutes_above_target(minutes, target) for nonnegative integers. Return 0 when minutes are at or below target; otherwise return the excess. It should not print or read input. Supply cases below, equal to and above the target, plus zero. Use the returned result in a sentence outside the function.
D. Shared lists
Start with history = [10, 20], then draft = history. Append 30 through draft and predict both lists. Repeat with draft = history.copy() starting from a fresh history. Explain which version supports trying a change without altering the original flat list.
E. Parsing versus validation
Classify each file content by the first failing stage: invalid JSON, wrong outer shape, wrong element type, outside the accepted range, or accepted history. Use the lab's integer range 1–240: broken, {}, [true], ["25"], [0], [240], []. Explain why an accepted empty history differs from a corrupt file treated as empty.
F. Diagnose before editing
A function intended to total sessions returns 5 for [5, 10, 5]. Propose two possible defects and choose a new input that distinguishes them. Give the predicted result under each hypothesis, then explain what line you would inspect after observing the result. Do not assume that a single failing output uniquely identifies a bug.