Python 3.08 Iterations

Completed the Popcorn and Homework Hacks using for, continue, break, counters, running totals, and thresholds.

Popcorn Hack: Theme Park Wait-Time Scanner

Code Runner Challenge

Popcorn Hack - theme park wait-time scanner

View IPYNB Source
# CODE_RUNNER: Popcorn Hack - theme park wait-time scanner
# Look at each ride's wait time (minutes) and compare it to two cutoffs.
# continue skips short waits; break stops at the first wait that means the ride should close.

wait_times = [10, 25, 5, 40, 15, 95, 30]

short_wait = 15
close_at = 90

long_count = 0
short_count = 0
total_long_minutes = 0
ride_closed = False

for wait in wait_times:
    if wait < short_wait:
        short_count += 1
        print(wait, "min: short wait, skip")
        continue

    long_count += 1
    total_long_minutes += wait
    print(wait, "min: long wait")

    if wait >= close_at:
        ride_closed = True
        print("Wait is too long - close the ride and stop scanning")
        break

print("Short waits skipped:", short_count)
print("Long waits:", long_count)
print("Total long-wait minutes:", total_long_minutes)
print("Ride closed:", ride_closed)
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 suits traversing a known list, continue skips only the current pass, and a while loop needs a condition that can change and eventually become false.

MCQ 3.08 Iterations: A, B, A, B
Score: 4/4

Homework Hack: Bakery Oven Monitor

The checker goes through nine oven temperature readings with two thresholds, continue, break, and several summary values.

Code Runner Challenge

Homework Hack - themed loop-based checker

View IPYNB Source
# CODE_RUNNER: Homework Hack - themed loop-based checker
# Check each oven temperature (°F) against a "too cool" and a "too hot" threshold.
# continue skips cool readings after counting them; break stops at the first dangerously hot one.
# Tracking variables build the final report for every reading seen before stopping.

temps = [350, 420, 300, 455, 390, 510, 430, 470, 380]
too_cool = 400
too_hot = 500

checked = 0
cool = 0
adjust = 0
adjust_total = 0
overheated = False

for temp in temps:
    checked += 1

    if temp < too_cool:
        cool += 1
        print(temp, "F: cool enough, no change")
        continue

    adjust += 1
    adjust_total += temp
    print(temp, "F: lower the heat")

    if temp >= too_hot:
        overheated = True
        print("Oven overheating! Stop checking.")
        break

print("Final Report")
print("Checked:", checked)
print("Cool:", cool)
print("Needed adjusting:", adjust)
print("Total adjusted temperature:", adjust_total)
print("Overheated:", overheated)
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...