Python Lab, Week 4: Choosing and Writing Loops
Lab length: 90 minutes · Group: 24 first-year students · Outcome: Week 4 — use definite and indefinite iteration to solve problems on lists and strings · Follows the 50-minute loops lecture
Objective and success criteria
Objective: Students choose between a for loop and a while loop and write each correctly to process a list.
| Band | Success criterion (I can…) |
|---|---|
| Support | …trace a given for loop over a list and predict its output line by line. |
| Core | …write a for loop that totals or filters a list, and a while loop that stops on a condition, and say why each loop type fits. |
| Stretch | …rewrite a while loop as a for loop (and back) and avoid off-by-one and infinite-loop bugs. |
Timed lab sequence
| Time | Phase | TA does | Students do | Check |
|---|---|---|---|---|
| 0–8 | Hook | Project: "You have a list of 1,000 sensor readings. Find the first one over 50. Which loop would you use, and why?" | Discuss in pairs; type an answer in the shared lab sheet. | Read 5 answers aloud; tally "for" vs "while". |
| 8–23 | Model (I do) | Live-code two solutions with think-aloud: a for loop that totals a list; a while loop that finds the first reading over 50. Introduce a deliberate bug (i <= len(readings)) and fix it. | Follow along, run the same code. | Ask: "What would happen if I forgot i += 1?" Cold-call 2 students. |
| 23–43 | Guided (we do) | Tasks 1–2 on the lab sheet in pairs (driver/navigator, swap at minute 33). Ask Hinge 1 at minute 35. | Task 1: count even numbers. Task 2: keep asking for input until "quit". | Hinge 1 via show of hands; if < 75% correct, re-model range boundaries. |
| 43–75 | Independent (you do) | Circulate: visit students who froze on blank editors first (minutes 43–50), then answer the queue. | Tasks 3–5 solo, with optional hints revealed in the sheet. | Check each student has a running Task 3 by minute 60. |
| 75–83 | Hinge 2 + review | Ask Hinge 2; show two student solutions (with permission), one for and one while. | Vote, then explain choices. | Note who still picks while for a fixed-size list. |
| 83–90 | Exit ticket | Post 3 questions in the lab sheet. | Answer individually before leaving. | Review results before the next lab. |
Total: 90 minutes. TA talk ≈ 25 minutes — under a third of the lab.
Starter code for the model
python
readings = [12, 48, 51, 7, 66]
# Definite iteration: we know we visit every item.
total = 0
for value in readings:
total += value
print("Total:", total)
# Indefinite iteration: stop as soon as the condition is met.
i = 0
while i < len(readings) and readings[i] <= 50:
i += 1
print("First reading over 50:", readings[i] if i < len(readings) else "none")Hinge questions
- **How many times does
for n in range(1, 5):run?* Likely wrong answer: *5. It reveals the belief thatrangeincludes its stop value. - You must process every word in a 300-word essay. Which loop? Likely wrong answer: while. It reveals students who do not yet link "known collection" to
for.
Differentiation
| Group | Task | Support |
|---|---|---|
| Support (students who freeze on blank editors) | Tasks 3–4 as fill-in-the-gaps code with comments marking each missing line. | Trace table template; "predict, run, explain" prompt card |
| Core | Tasks 3–5 from scratch. | Hidden hints in the lab sheet, one per task |
| Stretch (students who coded before) | Task 6: rewrite Task 2 with for and break, then explain which is clearer; add input validation. | None needed; ask them to pair-explain to a neighbour after finishing |
Exit ticket (with answers)
- What does this print?
for c in "hey": print(c.upper())Answer: H, E, Y on separate lines. - Why might
while x != 10: x += 3never stop ifxstarts at 0? **Answer: x goes 0, 3, 6, 9, 12… and never equals 10; usex < 10.** - Pick the loop: "Ask for a password until it is correct." Answer: while — the number of attempts is unknown.
Likely misconceptions and fixes
- **
range(1, 5)includes 5.** Fix: printlist(range(1, 5))live. - **Forgetting to update the loop variable in
while.** Fix: teach the "initialise, test, update" checklist; show the infinite loop and how to stop it (Ctrl+C). - Modifying a list while looping over it. Fix: build a new list instead; demo the skipped item.
- **Using indexes in
forwhen not needed.** Fix: comparefor i in range(len(xs))withfor x in xs.
Reflection prompts
- How many students reached a running Task 3 by minute 60?
- Which hinge question split the room most?
- Did the pair swap at minute 33 keep both students typing? [check with module lead]
Next steps I can help with: I can write the full lab sheet with starter code, expected output and hidden hints, or list the six most common loop bugs with diagnostic questions. How did the lab go?