flowchart TD
Init["1. Initialize State<br/>total = 0 | count = 0"]
Next{"2. Next item left<br/>or condition True?"}
Body["3. Run Loop Body<br/>Inspect current item"]
Skip{"Hit continue?"}
Stop{"Hit break?"}
Update["4. Update State<br/>total += val | count += 1"]
Done["5. Loop Finished<br/>Use final state"]
Init --> Next
Next -- "Yes" --> Body
Body --> Skip
Skip -- "Yes (skip rest)" --> Next
Skip -- "No" --> Stop
Stop -- "Yes (exit early)" --> Done
Stop -- "No" --> Update
Update --> Next
Next -- "No (exhausted)" --> Done
5 Loops, Iteration Patterns, Accumulators, and Execution Tracing
Chapter Mission: Automate repetitive epidemiological calculations across hundreds or millions of patient records using
forandwhileloops, master the 4 Universal Loop Patterns (Accumulator, Counter, Filter, Running Max/Min), control iteration flow withrange(),enumerate(),continue, andbreak, and trace every variable state on paper using Loop Execution Trace Tables.
Chapter Companion Notebooks (1-to-1 Mirrored in Google Colab)
| Lab Type | Interactive Colab Notebook | Scan (Print) |
|---|---|---|
| Walkthrough | ch05 / 01_walkthrough.ipynb | ![]() |
| Guided To-Dos | ch05 / 02_guided_todos.ipynb | ![]() |
| Coding Exam | ch05 / 03_coding_exam.ipynb | ![]() |
Learning Objectives
By the end of this chapter, you will be able to:
- Iterate cleanly over sequences and integer ranges using
forloops and the half-open intervalrange(start, stop, step). - Implement the 4 Universal Loop Patterns—Accumulator, Counter, Filter, and Running Max/Min—in a single clean pass over clinical data.
- Skip DHS missing-value sentinels (
999,None) withcontinueand exit outbreak scans early withbreak. - Track human-readable 1-based weeks or days alongside values using
enumerate(..., start=1). - Model unknown-duration processes—such as epidemic doubling and hospital bed surge capacity—using condition-driven
whileloops without creating infinite loops. - Build Loop Execution Trace Tables to verify nested loops, catch off-by-one bugs, and prevent
TypeError,NameError, andZeroDivisionError.
5.1 Stage 1 — First-Principles Intuition: The Triage Conveyor Belt
In Chapter 2, you totaled 4 days of dengue cases by writing four copy-pasted lines: total_cases += day_1, += day_2, += day_3, += day_4. That works for 4 days—and collapses completely when your thesis analyzes 365 days of national surveillance across 64 districts or 20,000 mothers in the Bangladesh DHS.
Loops let you write the clinical rule once and instruct Python to apply it faithfully to every patient record automatically.
Picture two different stations in a district hospital:
- The
forLoop (Fixed Stack of Patient Charts): A triage nurse receives a finite stack of charts (hb_readings). She picks up Chart 1, checks hemoglobin, updates her clipboard tallies (total_hb,anemic_count), sets it down, and repeats for Chart 2, Chart 3, …, stopping automatically when the stack is empty. - The
whileLoop (Emergency Ward Bed Monitor): A hospital director watches active dengue admissions climb day by day. She does not know in advance whether beds will run out on Day 6 or Day 14—she simply keeps simulating “as long asoccupied_beds <= bed_capacity,” stopping the moment capacity is breached.
5.1.1 Choosing for vs. while
| Loop Type | When to Choose It | Public Health Example |
|---|---|---|
for loop |
You have a known collection (list of readings) or exact count (range(1, 8)). |
Computing clean mean hemoglobin across 500 survey rows. |
while loop |
You do not know how many steps it will take until a condition becomes False. |
Simulating days until epidemic doubling overflows 200 hospital beds. |
5.2 Stage 2 — Visual Mental Model: The Loop Execution Cycle
Every loop in Python—whether processing 5 clinic rows or 50 million genomic reads—follows the exact same cyclic execution engine:
Golden Rule of Loop Initialization: Always initialize accumulators (
total = 0), counters (count = 0), and filter lists (results = []) ONCE before the loop begins. Puttingtotal = 0inside the loop body wipes out your progress on every iteration; forgetting it entirely triggers aNameError!
5.3 Stage 3 — Line-by-Line Code Walkthrough & Error Anatomy
Open part_01_python_foundations/ch05_loops_tracing/01_walkthrough.ipynb in Colab and execute each cell while reading the step-by-step Loop Execution Trace Tables below.
5.3.1 for Loops & range() (Half-Open Intervals)
Python’s range(start, stop, step) follows the half-open mathematical interval \([start, stop)\)—it includes start and stops one step before stop.
Expected Output:
5.3.2 The 4 Universal Loop Patterns
Over 90% of real-world data processing loops are combinations of just four universal patterns:
- Accumulator: Running sum (
total_hb += hb), initialized to0or0.0. - Counter: Running tally of matching records (
anemic_count += 1), initialized to0. - Filter: Growing list of qualifying items (
anemic_cases.append(hb)), initialized to[]. - Running Max / Min: Tracking the extreme value seen so far (
max_hb = hb_readings[0]).
hb_readings = [10.4, 12.1, 9.6, 11.8, 10.9]
total_hb = 0.0 # Pattern 1: Accumulator
anemic_count = 0 # Pattern 2: Counter
anemic_cases = [] # Pattern 3: Filter
max_hb = hb_readings[0] # Pattern 4: Running Max
for hb in hb_readings:
total_hb += hb
if hb < 11.0:
anemic_count += 1
anemic_cases.append(hb)
if hb > max_hb:
max_hb = hb
mean_hb = round(total_hb / len(hb_readings), 2)
print("Mean Hb:", mean_hb, "| Anemic count:", anemic_count)
print("Anemic list:", anemic_cases, "| Highest Hb:", max_hb)Expected Output:
Loop Execution Trace Table (Exact State After Each Iteration):
| Iter | hb |
total_hb |
hb < 11.0? |
anemic_count |
anemic_cases |
max_hb |
|---|---|---|---|---|---|---|
Init |
— | 0.0 |
— | 0 |
[] |
10.4 |
1 |
10.4 |
10.4 |
True |
1 |
[10.4] |
10.4 |
2 |
12.1 |
22.5 |
False |
1 |
[10.4] |
12.1 |
3 |
9.6 |
32.1 |
True |
2 |
[10.4, 9.6] |
12.1 |
4 |
11.8 |
43.9 |
False |
2 |
[10.4, 9.6] |
12.1 |
5 |
10.9 |
54.8 |
True |
3 |
[10.4, 9.6, 10.9] |
12.1 |
5.3.3 continue & break (DHS Missing Codes & Early Outbreak Alert)
Real epidemiology datasets are never pristine. Demographic and Health Surveys (DHS) routinely encode “refused / not measured” as 999 or None. Use continue to skip bad rows immediately, and break to stop an alert scan the instant a threshold is crossed:
# Part A: continue skips DHS missing sentinels (999 or None)
raw_sbp = [122, 999, 148, None, 134, 999]
valid_total = 0
valid_count = 0
for sbp in raw_sbp:
if sbp is None or sbp == 999:
continue
valid_total += sbp
valid_count += 1
print("Clean SBP mean:", round(valid_total / valid_count, 1))
# Part B: break halts scan on FIRST outbreak threshold breach (>= 50)
daily_cases = [14, 28, 53, 61, 40]
first_alert_day = None
for day_num, cases in enumerate(daily_cases, start=1):
if cases >= 50:
first_alert_day = day_num
break
print("First outbreak alert on Day:", first_alert_day)Expected Output:
Loop Execution Trace Table — Part A (continue Skipping Missing Sentinels):
| Iter | sbp |
Skipped (continue)? |
valid_total |
valid_count |
|---|---|---|---|---|
Init |
— | — | 0 |
0 |
1 |
122 |
No | 122 |
1 |
2 |
999 |
Yes | 122 |
1 |
3 |
148 |
No | 270 |
2 |
4 |
None |
Yes | 270 |
2 |
5 |
134 |
No | 404 |
3 |
6 |
999 |
Yes | 404 |
3 |
is None Before Arithmetic
Notice we wrote if sbp is None or sbp == 999:. Checking is None first is good defensive habit: because of Python’s short-circuit or, if sbp is None, Python skips immediately without ever attempting numeric comparisons like None < 11.0 (which would crash with TypeError)!
5.3.4 enumerate() for Peak Dengue Week Identification
Whenever you need both the position (e.g., Week 1, Week 2, …) and the value, reach for enumerate(sequence, start=1). Passing start=1 aligns Python’s 0-based indexing with human epidemiological week numbers:
# Weekly dengue cases across 6 monsoon weeks
weekly_cases = [32, 48, 79, 115, 94, 61]
peak_week = 1
peak_cases = weekly_cases[0]
for week, cases in enumerate(weekly_cases, start=1):
if cases > peak_cases:
peak_cases = cases
peak_week = week
print(f"Peak dengue week: Week {peak_week} ({peak_cases} cases)")Expected Output:
5.3.5 while Loops: Epidemic Doubling & Hospital Bed Capacity
When you want to simulate forward in time until a clinical threshold is breached—without knowing how many steps it will take—use a while loop:
Expected Output:
Loop Execution Trace Table (while active_cases <= 200):
| Iter | Check active_cases <= 200 |
active_cases After |
days After |
|---|---|---|---|
Start |
25 <= 200 (True) |
25 |
0 |
1 |
50 <= 200 (True) |
50 |
3 |
2 |
100 <= 200 (True) |
100 |
6 |
3 |
200 <= 200 (True) |
200 |
9 |
4 |
400 <= 200 (False \(\rightarrow\) EXIT) |
400 |
12 |
If you forget to update the state variable inside a while loop (for instance, forgetting active_cases *= 2), the condition 25 <= 200 stays True forever! In Google Colab, click the Stop (■) button on the cell—and always verify on paper that every while body moves strictly closer to terminating.
5.3.6 Nested Loops: Multi-Ward Surveillance Summary
Real health data is hierarchical: clinics inside districts, days inside wards, visits inside patients. A nested loop places one loop inside another. Watch carefully where ward_total = 0 is reset:
# Each inner list holds 3 daily dengue admissions for one ward
ward_admissions = [
[6, 9, 11], # Ward 1 -> sum 26
[14, 18, 12], # Ward 2 -> sum 44
]
grand_total = 0
for ward_num, daily_counts in enumerate(ward_admissions, start=1):
ward_total = 0
for count in daily_counts:
ward_total += count
grand_total += ward_total
print(f"Ward {ward_num} 3-day total: {ward_total}")
print("Hospital grand total:", grand_total)Expected Output:
Notice the two accumulator scopes: grand_total = 0 is initialized outside both loops (accumulates the entire hospital), while ward_total = 0 is reset inside the outer loop so Ward 2 does not inherit Ward 1’s 26 admissions!
5.3.7 Break It On Purpose! (Loop Error Anatomy)
Python’s Traceback:
Diagnosis: A single integer (5) is a scalar object, not a sequence of items. Python cannot loop over a single number directly.
Fix: Wrap the count in range(5) (or range(1, 6) for 1-based days): for day in range(1, 6):.
Python’s Traceback:
Diagnosis: uninitialized_total += cases means uninitialized_total = uninitialized_total + cases. On the very first iteration, Python looks up uninitialized_total on the right-hand side and discovers it does not exist yet!
Fix: Initialize uninitialized_total = 0 on the line above the for header.
Python’s Traceback:
Diagnosis: Every reading hit continue, leaving valid_n == 0. Dividing 0.0 / 0 crashes your entire pipeline on the first empty clinic subgroup!
Fix: Always guard the division: mean_missing = round(hb_sum / valid_n, 2) if valid_n > 0 else None.
5.4 Stage 4 — The Researcher’s Problem-Structuring Blueprint
Scenario: A rural maternal health camp collects 7 hemoglobin readings (in g/dL), but two assays failed and were coded as 999 and None: raw_hb = [10.2, 999, 11.6, None, 9.4, 12.3, 10.8]. In a single clean pass, skip missing codes, compute valid_count, mean_hb (rounded to 2 decimals, safely returning None if no valid readings exist), anemic_count (\(< 11.0\text{ g/dL}\)), and min_hb (the lowest valid hemoglobin reading to flag the most severe case).
5.4.1 Blueprint (Completed on Paper Before Coding)
- Inputs & Units: List
raw_hbcontaining floats (g/dL), DHS missing code999, and PythonNone. - Edge Cases & Traps:
- Initializing
min_hb = raw_hb[0]is dangerous if the first record happens to be999orNone! Initializemin_hb = Noneand update whenevermin_hb is None or hb < min_hb. - Dividing by
len(raw_hb)(7) instead ofvalid_count(5) biases the mean downward. - Guard
valid_count > 0before dividing to preventZeroDivisionError.
- Initializing
- Step-by-Step Algorithm:
- Initialize
valid_sum = 0.0,valid_count = 0,anemic_count = 0,min_hb = None. - Loop
for hb in raw_hb:; ifhb is None or hb == 999:,continue. - Update
valid_sum += hb,valid_count += 1, checkhb < 11.0, and updatemin_hb. - After the loop, compute
mean_hb = round(valid_sum / valid_count, 2)ifvalid_count > 0elseNone.
- Initialize
- Verification Plan (Hand Check):
- Valid readings: \(10.2, 11.6, 9.4, 12.3, 10.8\) (
valid_count = 5). - Sum: \(10.2 + 11.6 + 9.4 + 12.3 + 10.8 = 54.3\); Mean: \(54.3 / 5 = 10.86\).
- Anemic (\(< 11.0\)): \(10.2, 9.4, 10.8 \Rightarrow 3\); Minimum: \(9.4\).
- Valid readings: \(10.2, 11.6, 9.4, 12.3, 10.8\) (
5.4.2 Clean Python Implementation (Verbatim in 01_walkthrough.ipynb)
# Step 1: Raw maternal hemoglobin readings with DHS missing codes
raw_hb = [10.2, 999, 11.6, None, 9.4, 12.3, 10.8]
# Step 2: Initialize accumulators, counters, and safe running min
valid_sum = 0.0
valid_count = 0
anemic_count = 0
min_hb = None
# Step 3: Single-pass cleaning + summary loop
for hb in raw_hb:
if hb is None or hb == 999:
continue
valid_sum += hb
valid_count += 1
if hb < 11.0:
anemic_count += 1
if min_hb is None or hb < min_hb:
min_hb = hb
# Step 4: Guard against ZeroDivisionError before computing mean
if valid_count > 0:
mean_hb = round(valid_sum / valid_count, 2)
else:
mean_hb = None
print(f"Valid: {valid_count} | Mean Hb: {mean_hb} g/dL")
print(f"Anemic: {anemic_count} | Most severe (Min Hb): {min_hb} g/dL")Output:
5.5 Stage 5 — Guided “To-Do” Practice Labs
Open part_01_python_foundations/ch05_loops_tracing/02_guided_todos.ipynb in Google Colab, run Step 0 to activate sharukh-tutor, and complete all six loop exercises below:
- To-Do 5.1 (Warm-Up — Accumulator & Counter): Given
daily_doses = [45, 52, 48, 60, 55, 40, 50], use aforloop to computetotal_doses(int) andhigh_volume_days(count of days with \(\ge 50\) doses). - To-Do 5.2 (Filter Pattern): Given
temps_c = [36.8, 38.4, 37.5, 39.1, 38.0, 36.9], build a new listfebrile_tempscontaining only temperatures \(\ge 38.0^\circ\text{C}\). - To-Do 5.3 (
continuePattern — DHS Missing Codes): Givenraw_ages = [24, 999, 31, None, 28, 999, 37], skipNoneand999withcontinue, then computevalid_count(int) andmean_agerounded to 1 decimal place. - To-Do 5.4 (
break+enumerate()— First Outbreak Breach): Givendaily_cases = [18, 29, 44, 52, 67, 58]andalert_threshold = 50, loop withenumerate(daily_cases, start=1)andbreakon the first day \(\ge 50\) to capturebreach_dayandbreach_cases. - To-Do 5.5 (
whileLoop — Epidemic Doubling): Starting fromcases = 15,bed_limit = 200,days_elapsed = 0, doublecasesand add2todays_elapsedwhilecases <= bed_limit. - To-Do 5.6 (Nested Loops — Multi-District Clinic Totals): Given
district_clinics = [[12, 18, 15], [22, 19, 25], [10, 14, 16]], computedistrict_totals(sum of each inner list) andtop_district(1-based district number with the highest total).
5.6 Stage 6 — Concept & Execution Trace Test (No Computer Allowed!)
Trace every loop by hand on paper before checking Stage 8:
range()Half-Open Interval: What exact list doeslist(range(2, 10, 2))produce? Why is10excluded?Accumulator Trace Table: Trace
totalafter every iteration:continuevs.break: What exact numbers are printed by:Reset-Inside-Loop Bug: A student places
total = 0inside aforloop right beforetotal += xwhen iterating over[10, 20, 30]. What final value doestotalhold, and why?enumerate()Start Parameter: What tuple(idx, val)doesenumerate(["A", "B", "C"], start=1)yield on its second iteration?Boundary Condition (
<vs.<=): Forcases = 50; while cases < 200: cases *= 2, what is the final value ofcases? What changes if the condition iscases <= 200?Spot the Exception #1: Why does
for i in 10:raiseTypeError: 'int' object is not iterable, and how do you fix it?Spot the Exception #2: Why does computing
hb_sum / valid_countafter skipping999values riskZeroDivisionError, and how should you guard it?Nested Loop Iterations: If an outer loop iterates over
4districts and an inner loop iterates over7days, how many total times does the innermost statement execute?Safe Running Minimum: Why is initializing
min_hb = 0a silent bug when searching for the lowest hemoglobin in[10.2, 9.4, 11.8], and how should you initializemin_hbinstead?
5.7 Stage 7 — Chapter 5 Hands-On Coding Exam (100 Points)
Open part_01_python_foundations/ch05_loops_tracing/03_coding_exam.ipynb, complete all three tasks, and run tutor.check_exam("ch05", exam_submissions) to grade your work out of 100 points.
5.7.1 Exam Task 5.1 (30 Points): Dirty Maternal Hemoglobin Cleaning & Summary
Given raw maternal hemoglobin readings raw_hb_readings = [10.5, 999, 11.8, None, 9.2, 12.4, 999, 10.1, None, 11.0], use a for loop with continue to skip None and 999, and compute:
valid_count(int, number of valid readings)missing_count(int, number of skippedNoneor999entries)mean_hb(float, mean of valid readings rounded to 2 decimal places, guarded againstvalid_count == 0)anemic_count(int, count of valid readings strictly< 11.0g/dL)
5.7.2 Exam Task 5.2 (35 Points): Monsoon Dengue Peak-Week & High-Alert Surveillance
A district records 8 consecutive weeks of monsoon dengue cases: weekly_dengue = [42, 65, 98, 134, 178, 162, 115, 74] with high_alert_threshold = 100. Using enumerate(weekly_dengue, start=1), compute:
total_cases(int, cumulative 8-week cases)peak_week(int, 1-based week number with maximum cases)peak_cases(int, maximum weekly case count)high_alert_weeks(int, count of weeks withcases >= 100)first_alert_week(int, 1-based week number of the first week withcases >= 100)
5.7.3 Exam Task 5.3 (35 Points): Hospital Surge-Capacity Day Simulation (while Loop)
A district hospital starts on Day 0 with occupied_beds = 40, bed_capacity = 250, daily_admissions = 20 (for Day 1), and daily_discharges = 10 (constant daily recoveries). Simulate with while occupied_beds <= bed_capacity:, executing these exact steps each day:
- Increment
surge_day += 1 - Update
occupied_beds += daily_admissions - daily_discharges - Accumulate
total_admitted += daily_admissions - Increase
daily_admissions += 5for the following day
After the loop exits, report surge_day, final_occupied_beds (occupied_beds), overflow_beds (occupied_beds - bed_capacity), and total_admitted.
5.8 Stage 8 — Complete Annotated Solutions & Debugging Post-Mortem
5.8.1 A. Concept & Execution Trace Test Key
[2, 4, 6, 8].range(start, stop, step)is half-open \([2, 10)\), so it stops strictly before10.0\(\rightarrow\)8(\(+4 \times 2\)) \(\rightarrow\)22(\(+7 \times 2\)) \(\rightarrow\)28(\(+3 \times 2\)).- Prints
10and20only (999is skipped bycontinue;-1triggersbreak, so30is never reached). totalends at30(the last item only), becausetotal = 0wipes out the accumulated sum at the start of each iteration.(2, "B")(iteration 1 yields(1, "A"), iteration 2 yields(2, "B")).- With
< 200:50\(\rightarrow\)100\(\rightarrow\)200(stops at200). With<= 200:200 <= 200is stillTrue, so it doubles once more to400. 10is a singleint, not an iterable sequence. Fix:for i in range(10):.- If every reading is
999,valid_countstays0, sohb_sum / 0raisesZeroDivisionError. Guard withif valid_count > 0: ... else: None. - \(4 \times 7 = 28\) total executions.
- Because every valid hemoglobin reading (\(> 0\)) is larger than
0,hb < min_hbis neverTrueandmin_hbstays stuck at0! Initializemin_hb = None(and checkif min_hb is None or hb < min_hb:) ormin_hb = valid_list[0].
5.8.2 B. Annotated Solutions — Guided To-Dos (Mirrored in 02_guided_todos_SOLUTION.ipynb)
# To-Do 5.1
daily_doses = [45, 52, 48, 60, 55, 40, 50]
total_doses = 0
high_volume_days = 0
for doses in daily_doses:
total_doses += doses
if doses >= 50:
high_volume_days += 1 # 350, 4
# To-Do 5.2
temps_c = [36.8, 38.4, 37.5, 39.1, 38.0, 36.9]
febrile_temps = []
for temp in temps_c:
if temp >= 38.0:
febrile_temps.append(temp) # [38.4, 39.1, 38.0]
# To-Do 5.3
raw_ages = [24, 999, 31, None, 28, 999, 37]
age_sum = 0
valid_count = 0
for age in raw_ages:
if age is None or age == 999:
continue
age_sum += age
valid_count += 1
mean_age = round(age_sum / valid_count, 1) # 4, 30.0
# To-Do 5.4
daily_cases = [18, 29, 44, 52, 67, 58]
breach_day, breach_cases = None, None
for day, cases in enumerate(daily_cases, start=1):
if cases >= 50:
breach_day, breach_cases = day, cases
break # Day 4, 52
# To-Do 5.5
cases, bed_limit, days_elapsed = 15, 200, 0
while cases <= bed_limit:
cases *= 2
days_elapsed += 2 # Day 8, 240
# To-Do 5.6
district_clinics = [[12, 18, 15], [22, 19, 25], [10, 14, 16]]
district_totals = []
top_district, best_total = 1, -1
for d_num, clinics in enumerate(district_clinics, start=1):
d_total = 0
for count in clinics:
d_total += count
district_totals.append(d_total)
if d_total > best_total:
best_total, top_district = d_total, d_num # [45, 66, 40], 25.8.3 C. Annotated Solutions — Chapter 5 Coding Exam (Mirrored in 03_coding_exam_SOLUTION.ipynb)
# Exam Task 5.1 (30 pts)
raw_hb_readings = [
10.5, 999, 11.8, None, 9.2, 12.4, 999, 10.1, None, 11.0
]
valid_count = 0
missing_count = 0
hb_sum = 0.0
anemic_count = 0
for hb in raw_hb_readings:
if hb is None or hb == 999:
missing_count += 1
continue
hb_sum += hb
valid_count += 1
if hb < 11.0:
anemic_count += 1
mean_hb = round(hb_sum / valid_count, 2) if valid_count > 0 else None
# valid_count=6, missing_count=4, mean_hb=10.83, anemic_count=3
# Exam Task 5.2 (35 pts)
weekly_dengue = [42, 65, 98, 134, 178, 162, 115, 74]
total_cases = 0
peak_week = 1
peak_cases = weekly_dengue[0]
high_alert_weeks = 0
first_alert_week = None
for week, cases in enumerate(weekly_dengue, start=1):
total_cases += cases
if cases > peak_cases:
peak_cases = cases
peak_week = week
if cases >= 100:
high_alert_weeks += 1
if first_alert_week is None:
first_alert_week = week
# total=868, peak_week=5, peak_cases=178, alerts=4, first_alert=4
# Exam Task 5.3 (35 pts)
occupied_beds = 40
bed_capacity = 250
daily_admissions = 20
daily_discharges = 10
surge_day = 0
total_admitted = 0
while occupied_beds <= bed_capacity:
surge_day += 1
occupied_beds += daily_admissions - daily_discharges
total_admitted += daily_admissions
daily_admissions += 5
final_occupied_beds = occupied_beds # 260
overflow_beds = occupied_beds - bed_capacity # 10
# surge_day=8, final_occupied_beds=260, overflow_beds=10, total=3005.8.4 D. Debugging Post-Mortem: Top 3 Chapter 5 Failure Modes
- Dividing by
len(raw_list)Instead ofvalid_count: After skipping missing999andNonerecords withcontinue, dividinghb_sumbylen(raw_hb_readings)(10) instead ofvalid_count(6) silently fabricates a catastrophic mean hemoglobin of 6.50 g/dL instead of 10.83 g/dL—zero error message, completely corrupted research! - Overwriting First-Occurrence Alerts: In Exam Task 5.2, setting
first_alert_week = weekunconditionally insideif cases >= 100:overwrites Week 4 with Week 5, Week 6, and finally Week 7. Guarding withif first_alert_week is None:latches onto the earliest breach while letting the loop complete the remaining totals. - Statement-Order Drift Inside
whileLoops: In Exam Task 5.3, movingdaily_admissions += 5abovetotal_admitted += daily_admissionscounts Day 2’s admissions (25) on Day 1—inflatingtotal_admittedfrom 300 to 340. Always sketch a 2-row Loop Execution Trace Table whenever awhileloop updates coupled state variables!


