3.08 Iterations

Source lesson: python/iterations

Learning Objective: Use for loops with continue and break to scan data, skip values, count, total and stop early.

Submission notes

  • Lesson: Python 3.08 Iterations
  • MCQ 3.08: 4/4 answers: A,B,A,B
  • Popcorn: completed loop variable, thresholds, counters, continue, and break (yes)
  • Homework: list length = 10
  • Homework: thresholds = 50, 150
  • Homework: final checked count = 8
  • Homework: final action count = 3
  • Homework: critical found = yes

Popcorn Hack: Wildfire Dispatch Scanner

Filled every blank in the lesson’s starter: safe_cutoff = 3, critical_cutoff = 8, loop over alerts, += on the counters and running total, continue for safe alerts, critical_found = True, and break.

Code Runner Challenge

Popcorn Hack - wildfire dispatch scanner

View IPYNB Source
# CODE_RUNNER: Popcorn Hack - wildfire dispatch scanner
# Theme: The SFSRC team scans wildfire alert levels from different zones.
# Goal: skip safe zones, count response zones, total their alert scores, and stop at the first critical zone.

alerts = [1, 4, 2, 6, 3, 9, 5]

# Alerts below this number are skipped.
safe_cutoff = 3

# The loop stops at this number or higher.
critical_cutoff = 8

# Counters and totals start at 0 because no alerts have been processed yet.
action_count = 0
safe_count = 0
total_action_score = 0
critical_found = False

for alert in alerts:
    # Safe alert: count it, print a message, and skip to the next alert.
    if alert < safe_cutoff:
        safe_count += 1
        print("Alert " + str(alert) + ": safe, skipping")
        continue

    # If code reaches here, the alert needs action.
    action_count += 1
    total_action_score += alert
    print("Alert " + str(alert) + ": action needed")

    # Critical alert: mark it and stop the scan.
    if alert >= critical_cutoff:
        critical_found = True
        print("Critical alert found - stopping scan")
        break

# Final report
print("Safe alerts skipped: " + str(safe_count))
print("Action alerts: " + str(action_count))
print("Total action score: " + str(total_action_score))
print("Critical found: " + str(critical_found))
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

Trace: 1 skipped, 4 action, 2 skipped, 6 action, 3 action (3 is not below 3), then 9 is action and critical, so the loop breaks and 5 is never checked. 2 skipped, 4 action alerts, total 4 + 6 + 3 + 9 = 22, critical found.

MCQ Check

**MCQ 3.08: 4/4 answers: A,B,A,B**

1. Which loop is best when you already have a list of values to inspect? A for loop · B while loop · C break statement · D assignment statement Answer: A, a for loop goes through a known sequence one item at a time.

2. What does continue do inside a loop? A Stops the whole program · B Skips the rest of the current loop pass and moves to the next item · C Repeats the previous item · D Creates a new list Answer: B

3. What prevents a while loop from running forever? A A condition that eventually changes · B A longer variable name · C A print statement · D A list with more values Answer: A, something inside the loop has to make the condition become false.

4. A scan should stop as soon as it finds a critical alert. Which statement fits best? A continue · B break · C print · D else Answer: B, break exits the loop immediately.

Homework Hack: Air Quality Monitoring System

Theme: air quality sensors. 10 AQI readings, two thresholds (50 safe, 150 critical), a for loop using continue and break, and tracked summary values (checked, skipped, action, running total, critical found).

Code Runner Challenge

Homework Hack - Air Quality Monitoring System (AQI readings from 10 sensors)

View IPYNB Source
# CODE_RUNNER: Homework Hack - Air Quality Monitoring System (AQI readings from 10 sensors)
aqi_readings = [32, 45, 51, 38, 72, 105, 48, 155, 60, 210]

safe_cutoff = 50         # AQI 50 or below = good air, skip it
critical_cutoff = 150    # AQI 150 or above = unhealthy, stop and alert

checked_count = 0
skipped_count = 0
action_count = 0
total_score = 0
critical_found = False
critical_reading = None

for reading in aqi_readings:
    checked_count += 1
    if reading <= safe_cutoff:
        skipped_count += 1
        continue
    if reading >= critical_cutoff:
        critical_found = True
        critical_reading = reading
        break
    action_count += 1
    total_score += reading

print("===== AIR QUALITY REPORT =====")
print("Readings in list:", len(aqi_readings))
print("Safe cutoff:", safe_cutoff, "| Critical cutoff:", critical_cutoff)
print("Checked count:", checked_count)
print("Skipped (safe) count:", skipped_count)
print("Action count:", action_count)
print("Total action AQI:", total_score)
print("Critical found:", critical_found)
print("Critical reading:", critical_reading)
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

Reflection

The loop skips good readings (32, 45, 38, 48), counts action readings (51, 72, 105, total 228), and stops at the first critical reading (155), so 210 is never checked. continue saves work on safe data and break stops once an emergency is found.