Python 3.08: Iterations HW
Python 3.08 Iterations
The notebook includes Popcorn and Homework Hacks using for, continue, break, counters, running totals, and thresholds.
Popcorn Hack: Wildfire Dispatch Scanner
Code Runner Challenge
Popcorn Hack - wildfire dispatch scanner
View IPYNB Source
# CODE_RUNNER: Popcorn Hack - wildfire dispatch scanner
# Scan each wildfire alert and classify it using the cutoff values.
# continue skips safe fire_alerts; break stops the loop when a critical alert appears.
# Counters and a running total summarize the fire_alerts processed.
fire_alerts = [1, 4, 2, 6, 3, 9, 5]
safe_limit = 3
critical_limit = 9
action_total = 0
safe_total = 0
total_action_score = 0
found_critical = False
for alert in fire_alerts:
if alert < safe_limit:
safe_total += 1
print("Alert " + str(alert) + ": safe, skipping")
continue
action_total += 1
total_action_score += alert
print("Alert " + str(alert) + ": action needed")
if alert >= critical_limit:
found_critical = True
print("Critical alert found - stopping scan")
break
print("Safe fire_alerts skipped: " + str(safe_total))
print("Action fire_alerts: " + str(action_total))
print("Total action score: " + str(total_action_score))
print("Critical found: " + str(found_critical))
Lines: 1
Characters: 0
Output
Click "Run" in code control panel to see output ...
Quick Check and MCQ Answers
Reference Guide Quick Check: A, B, B — for is best for traversing a known list, continue skips only the current pass, and a while loop must have a condition that can change and eventually become false.
MCQ 3.08 Iterations: A, B, A, B
Score: 4/4
Homework Hack: Server Load Monitor
This checker uses nine values, two thresholds, continue, break, and multiple summary values.
Code Runner Challenge
Homework Hack - themed loop-based checker
View IPYNB Source
# CODE_RUNNER: Homework Hack - themed loop-based checker
# Check each server load using safe and critical thresholds.
# continue skips safe loads after counting them; break stops at a critical load.
# Tracking variables build the final report for everything checked before stopping.
load_readings = [22, 47, 35, 68, 51, 76, 44, 93, 59]
safe_limit = 40
critical_limit = 90
readings_checked = 0
action_total = 0
safe_skipped = 0
load_sum = 0
found_critical = False
for load in load_readings:
readings_checked += 1
if load < safe_limit:
safe_skipped += 1
print(str(load) + "% load: safe, no action")
continue
action_total += 1
load_sum += load
print(str(load) + "% load: action needed")
if load >= critical_limit:
found_critical = True
print("Critical server load found. Stop checking.")
break
print("Final Report")
print("Checked: " + str(readings_checked))
print("Skipped: " + str(safe_skipped))
print("Actions: " + str(action_total))
print("Total load score: " + str(load_sum))
print("Critical found: " + str(found_critical))
Lines: 1
Characters: 0
Output
Click "Run" in code control panel to see output ...