Pick a loop, build a program, prove it stops. Three starter programs — one while, one for, one that re-prompts — each with an accumulator or a counter doing real work.
No reading — build. Keep 3.1 (while), 3.3 (for / range), and 3.4 (accumulator) open. Choose which of the three programs below you’ll build first.
End of Week 1 in Programs. You put iteration to work: a
while loop that stops at a goal, a for loop that repeats a set number of
times, and an accumulator or counter tracking the answer.
Build one of these to full working order (do a second if you have time). Each one trains a different loop:
□ Savings-until-goal (while) — ask for a goal and a weekly deposit, then count the weeks until the balance reaches the goal. Accumulator + counter.
□ Times-table printer (for) — ask for a number and print its table from 1 to 12 with an f-string.
□ Password re-prompter (while) — keep asking until the user types the correct word, then let them in.
□ A purpose sentence comment at the top.
□ A loop with a clear escape hatch — prove it isn’t infinite.
□ At least one accumulator or counter (running total, week count, or attempt count).
□ Clear output with an f-string.
Copy the shape, not the numbers. Notice the three accumulator positions and the escape hatch:
# Counts the weeks to reach a savings goal. goal = int(input("Goal? ")) weekly = int(input("Per week? ")) balance = 0 # initialize weeks = 0 while balance < goal: balance = balance + weekly # update weeks = weeks + 1 # escape hatch moves it print(f"{weeks} weeks") # use
# purpose: what + why # inputs are numbers → int() # # two accumulators start at 0 # before the loop # loop WHILE not yet at goal # add this week's deposit # balance rising = escape hatch # use the count after the loop
Hover or tap a line to light its twin. The escape hatch here is line 6: the
balance keeps rising, so eventually balance < goal is false and the loop ends. Goal
200 at 50/week → 4 weeks.
Run the savings rules here before you trust your code. Set a goal and a weekly deposit; it shows the week count your program should print.
□ The working program (runs, loops, produces the right output).
□ Your purpose sentence at the top.
□ One sentence naming your escape hatch — the line that makes the loop eventually stop — and why it can’t run forever.
That escape-hatch sentence is the habit that prevents the most common loop bug there is, and it’s precise-process writing — Create PT practice.
Write the loop with its escape hatch first and run it on a tiny input (goal $60 at $25/week) so you can check the count by hand. Only once it stops correctly should you add nicer output or extra features. A loop that ends on the right pass beats a pretty one that runs forever.
Solo or paired — if you pair, swap driver and navigator every 10 minutes, and both of you must be able to point to the escape hatch.
Pick any starting number. If it’s even, halve it; if it’s
odd, do 3n + 1. Repeat until you reach 1, and count the steps. Nobody
has ever found a number that doesn’t eventually reach 1 — but nobody has proven it
always does, either. Your loop’s escape hatch is “stop when n is 1.”
Test it: starting at 6 takes 8 steps; starting at 7
takes 16. It’s a while loop, an if/else inside, and a
counting accumulator — all of Week 1 in six lines.