Unit 3 · Programming 2 · Lesson 3.5 · Lab

Lab: Loop Day

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.

Big Idea 3 — Algorithms & ProgrammingLab · 10 points

Before this lab

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.

1

Where We Are

People→ Bits→ Programs→ Internet→ Security→ Big Data→ Impact

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.

2

The Brief

Build one of these to full working order (do a second if you have time). Each one trains a different loop:

Pick your program

□ 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.

Requirements (whichever you pick)

□ 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.

3

The Model — savings-until-goal

Copy the shape, not the numbers. Notice the three accumulator positions and the escape hatch:

Python

# 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

What each part is doing

# 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.

4

Try It — Check Your Output

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.

goal = $ per week = $
Presets:
5

What You Turn In

Three things

□ 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.

6

How to Work

Make it stop before you make it fancy

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.

7

Challenge Tier

The Collatz step counter

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.

8

Connections

Uses all of Week 1 — while (3.1), the flip idea (3.2), for/range (3.3), and the accumulator (3.4).
Next in 3.6 — lists. One name for many values — the thing that makes loops truly powerful.
Returns in 3.10 — the gradebook lab pours a list into the accumulators you built today.
Returns on the exam — savings/countdown loops and step-counters are classic trace questions. If yours runs correctly, you can trace theirs.
← 3.4 The Accumulator