Python 3.08: Iterations HW
Popcorn Hack: Wildfire Dispatch Scanner
Every blank is filled in: the loop variable traverses alerts, safe_cutoff = 3, critical_cutoff = 8, the counters use +=, continue skips safe alerts, and break stops at the first critical alert.
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 3 are low enough to skip.
safe_cutoff = 3
# An alert of 8 or higher is critical, so the scan stops there.
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
# The loop variable `alert` takes each value in the list, one pass at a time.
for alert in alerts:
if alert < safe_cutoff:
safe_count += 1
print("Alert " + str(alert) + ": safe, skipping")
# continue skips the rest of THIS pass only, so safe alerts never reach the action code below.
continue
# If code reaches here, the alert needs action.
action_count += 1
total_action_score += alert
print("Alert " + str(alert) + ": action needed")
if alert >= critical_cutoff:
critical_found = True
print("Critical alert found - stopping scan")
# break ends the WHOLE loop, so the 5 after the 9 is never checked.
break
# Final report: these lines should explain what happened during the scan.
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))
Actual output:
Code Runner Challenge
Homework Hack - themed loop-based checker
View IPYNB Source
Alert 1: safe, skipping
Alert 4: action needed
Alert 2: safe, skipping
Alert 6: action needed
Alert 3: action needed
Alert 9: action needed
Critical alert found - stopping scan
Safe alerts skipped: 2
Action alerts: 4
Total action score: 22
Critical found: True
Trace: 1 and 2 are below 3, so continue skips them. 4, 6, 3, and 9 are counted (4 + 6 + 3 + 9 = 22). At 9, the critical check is true, so break ends the loop, and the final 5 is never checked.
MCQ 3.08
| MCQ 3.08: ___/4 | answers: ___ (paste the result line from the lesson’s MCQ check here) |
Homework Hack: School Web Server Load Monitor
Theme: a monitor for the school’s web servers. Each value is a server’s CPU load as a percent. Loads under 40% are normal and skipped, loads of 40% or more get an IT check-in, and the first load of 90% or more is critical, so the scan stops and IT fixes that server first.
It uses 10 values, 2 thresholds, a for loop, continue, break, and 5 tracked summary values.
# CODE_RUNNER: Homework Hack - themed loop-based checker
# Theme: School web server load monitor.
# Each value is a server's CPU load as a percent. Low loads are fine, medium loads need a
# check-in from IT, and a load at or above the critical cutoff means the server is about to
# crash, so the scan stops and IT focuses on that one server.
server_loads = [22, 35, 58, 41, 15, 67, 73, 94, 48, 30]
# Loads under 40% are normal, so they are skipped with continue.
safe_load_cutoff = 40
# A load of 90% or higher is critical, so the scan stops with break.
critical_load_cutoff = 90
# Summary variables start at 0 because no servers have been checked yet.
servers_checked = 0
servers_flagged = 0
servers_skipped = 0
total_flagged_load = 0
critical_found = False
for load in server_loads:
# Count every server the loop actually looks at, before any skip or stop.
servers_checked += 1
if load < safe_load_cutoff:
servers_skipped += 1
print(str(load) + "% load: normal, no action")
# continue jumps straight to the next server, so normal loads are never flagged or totaled.
continue
# Only loads at or above 40% reach this point.
servers_flagged += 1
total_flagged_load += load
print(str(load) + "% load: high, IT check-in needed")
if load >= critical_load_cutoff:
critical_found = True
print("Critical load found. Stop checking and fix this server first.")
# break ends the scan, so the last two servers are never checked.
# That's why servers_checked ends up smaller than the length of the list.
break
print("Final Report")
print("Servers in list: " + str(len(server_loads)))
print("Servers checked: " + str(servers_checked))
print("Normal servers skipped: " + str(servers_skipped))
print("Servers flagged: " + str(servers_flagged))
print("Sum of flagged loads: " + str(total_flagged_load))
print("Critical found: " + str(critical_found))
Actual output:
22% load: normal, no action
35% load: normal, no action
58% load: high, IT check-in needed
41% load: high, IT check-in needed
15% load: normal, no action
67% load: high, IT check-in needed
73% load: high, IT check-in needed
94% load: high, IT check-in needed
Critical load found. Stop checking and fix this server first.
Final Report
Servers in list: 10
Servers checked: 8
Normal servers skipped: 3
Servers flagged: 5
Sum of flagged loads: 333
Critical found: True
Why each loop tool is used:
forbecause the servers are a known list, and each one should be checked in order.continuebecause a normal server only needs to be skipped. The scan should keep going to the next server.breakbecause once one server is critical, IT should stop scanning and fix it. The two servers after it (48% and 30%) are never checked, which is why 8 were checked out of 10.- Accumulators (
servers_checked,servers_skipped,servers_flagged, andtotal_flagged_load) all start at 0 and grow with+=each time the matching code runs.