Week 5 — Coding Lab / Programming Studio · "Count, Total, Stop"
Course: Introduction to Computer Science — CS1 / Programming Fundamentals in Python (CSCI 1101) · Silver Oak University (fictional sample) · Prof. Okafor
Objective: Objective 4 — write a while loop with a counter and an accumulator; trace a loop's final value and iteration count; find and fix a loop bug · SLO A (write & run) + SLO B (trace & debug)
Worth 50 points · Coding Labs group = 15% of the grade · Coding Lab 5
Format: a hands-on programming studio worked in a free online Python environment — you'll (a) write a counter + accumulator, (b) trace several while loops and predict each one's final value and iteration count, and (c) find and fix a loop bug — and then catch the AI miscounting a loop.
This is the course's signature weekly component. Every instructional week has one Coding Lab. Everything runs in your browser — nothing to buy or install. The whole habit of this lab (and this course): don't guess what a loop does — run it and count what Python actually prints.
Safety note for this week: to study an infinite loop you read the buggy code; you do not run it as written. If a loop ever runs away from you for real, press Ctrl+C in a terminal or click Stop in the online editor to interrupt it.
Part 1 — The Big Picture
This week you learned the three parts every loop needs — a START, a STOP, and a STEP — and two patterns built on them: a counter that ticks up and an accumulator that builds a running total. This lab is those ideas, three ways: you'll write a counter + accumulator, trace loops (predict the final value and the iteration count, then run to check), and debug loops (find and fix an infinite loop and an off-by-one). By the end you'll be able to look at a while loop and say, before you press Run, exactly how many times it runs and what it ends with.
Your tools (both free, both in the browser):
- An online Python editor to write and run code: 🔗 https://www.online-python.com/ (or 🔗 https://www.programiz.com/python-programming/online-compiler)
- Python Tutor to watch a loop run step by step: 🔗 https://pythontutor.com/ — paste a loop, click Visualize Execution, then step forward and watch the counter change.
Part 2 — Setup (2 minutes)
- Open the online Python editor in a new tab.
- Delete any sample code so you have a blank editor.
- Type and run this loop:
python count = 1 while count <= 3: print(count) count = count + 1
You should see1,2,3in the output area. You just ran a loop three times. 🔁 (Notice: after the loop,countis 4 — one past the boundary.)
Part 3 — (a) Write: A Counter and an Accumulator
Write each of these in the editor, run it, and paste your code and its output into your submission.
- Count up. Write a program with a
whileloop that prints the numbers 1 through 10, one per line. (A counter that starts at 1 and steps by 1.) Run it and confirm you get ten lines. - Accumulate a total. Write a program that uses a
whileloop to add up the numbers from 1 to 10 and print the total. (An accumulator that starts at 0; a counter from 1 to 10.) Run it; write down the total it printed. - Count down. Write a program that counts down from 5 to 1, printing each number, then prints
Liftoff!. (A counter that starts at 5 and steps down by 1; stop when it reaches 0.) Run it.
Tip: for #2, initialize your accumulator (
total = 0) before the loop, and add the counter to it inside the loop.
Part 4 — (b) Trace: Predict, Then Run
For each loop below, first write your predicted output AND how many times the loop runs in the table (don't run it yet!). Then run each one in the editor and fill in the "Actual" columns. Where your prediction and the actual differ, that's the lesson — usually an off-by-one.
| # | Loop | Predicted output | Predicted # of runs | Actual output | Actual # of runs |
|---|---|---|---|---|---|
| 1 | i = 1 / while i <= 3: / print(i) / i = i + 1 |
______ | ______ | ______ | ______ |
| 2 | n = 3 / while n > 0: / print(n) / n = n - 1 |
______ | ______ | ______ | ______ |
| 3 | total = 0 / i = 1 / while i <= 4: / total = total + i / i = i + 1 / print(total) |
______ | ______ | ______ | ______ |
| 4 | c = 0 / while c <= 3: / c += 1 / print(c) |
______ | ______ | ______ | ______ |
| 5 | i = 10 / while i > 0: / print(i) / i = i - 3 |
______ | ______ | ______ | ______ |
| 6 | total = 0 / k = 0 / while True: / k += 1 / total += k / if total >= 20: / break / print(k, total) |
______ | ______ | ______ | ______ |
Hint for #4: the loop uses
<= 3. Watch carefully what valuecends on — it might be one more than you expect.
Hint for #6:while Truewould run forever, but thebreakstops it. Add it up: when does the running total first reach 20?
Visualize one: paste loop #1 (i = 1 / while i <= 3: print(i); i = i + 1) into Python Tutor and step through it. Watch the counter i climb to 4 and the condition flip to False — that's the moment the loop stops.
Part 5 — (c) Find & Fix the Bug
Each program below is broken. For each one, write (i) what's wrong, (ii) why it happens, and (iii) the fixed program. Run the FIXED version to confirm — but read the buggy versions carefully without running the infinite one.
Bug A — the loop that never stops (READ IT, do NOT run it as written).
count = 1
while count <= 3:
print(count)
# nothing else here
This is supposed to print 1, 2, 3 — but it never stops. Why? What one line fixes it? (If it ever runs for real, press Ctrl+C / Stop.)
Bug B — off-by-one.
n = 5
total = 0
i = 1
while i < n:
total = total + i
i = i + 1
print(total)
This is supposed to add up 1 through 5 (which is 15), but it prints something else. What's the bug, and how do you fix it?
Part 6 — Analysis Questions
Answer in a sentence or two each:
1. In the trace table, which prediction did you get wrong (if any) — and was it an off-by-one (counting the boundary wrong, or the ending value one off)? What did you learn from the gap?
2. In loop #4 (c = 0 / while c <= 3: / c += 1), why does c end at 4 and not 3? Explain the "step, then re-check the condition" order.
3. Loops #1 and #4 both involve the numbers up to 3, but one prints values and the other prints only a final number. What's the difference between a counter's printed values and a counter's final value?
4. For Bug A, the loop had a START and a STOP but was missing the STEP. In your own words, why does a missing counter update cause an infinite loop?
5. Connect it: Bug B (off-by-one) and the off-by-one you may have hit in the trace table both come down to one character — < vs <=. State the rule in your own words: when does each one include the boundary?
Part 7 — AI-Critique Moment (required — this is the BYOAI step)
Now bring in your approved chatbot (Gemini, Claude, or ChatGPT) and be the programmer who checks its work. Chatbots are genuinely weak at loops — they miscount iterations and get the off-by-one wrong, with total confidence.
- Paste this to the chatbot: "For each of these, tell me exactly what it prints, how many times the loop runs, and the final value of the loop variable: (1)
i = 1thenwhile i <= 5:print(i)i = i + 1. (2)i = 0thenwhile i <= 10:print(i)i = i + 2." - Check every claim by running each loop yourself in the editor and counting the lines:
- For (1): does it say the loop runs 5 times (correct) but also claimiends at 5? The final value ofiis actually 6 (the loop steps, then re-checks). Chatbots often get this ending value wrong.
- For (2): does it say 6 iterations (0, 2, 4, 6, 8, 10) — or wrongly say 5 (forgetting 0) or 11? Run it and count. - Write 2–3 sentences reporting what the AI got right and at least one thing you had to correct or verify carefully (most often: the final value of
i, or the iteration count being off by one). If it happened to get everything right, say how you confirmed each one by running it and counting — that's the skill.
The habit all term: the tool drafts, you run it and count. A chatbot will confidently miscount a loop — catching it by running the code is the entire point of a course about tracing and debugging.
Part 8 — What to Submit
Submit a single document (or text entry) with: your Part 3 programs and their outputs; your completed Part 4 trace table (predictions + both "Actual" columns); your Part 5 bug answers (what's wrong, why, and the fix for each); your Part 6 answers; and your Part 7 AI-critique paragraph. Due Sunday, Oct 4, 11:59 p.m. (50 points).
Instructor answer key & model outputs — REMOVE BEFORE PUBLISHING TO STUDENTS
Execution gate: PASS — every output below was produced by actually running the code in Python (Bug A is read, not run; its fixed form is run-verified). Students' Part 3 wording/variable names vary; grade those on "does it run and produce the right kind of output."
Part 3 (model, run-verified):
1. Count up 1–10, e.g. i = 1 / while i <= 10: / print(i) / i = i + 1 → prints 1 through 10 (ten lines). ✓ (i < 11 also fine)
2. Accumulate 1–10: total = 0 / i = 1 / while i <= 10: / total = total + i / i = i + 1 / print(total) → 55. ✓
3. Count down 5→1: n = 5 / while n > 0: / print(n) / n = n - 1 / print("Liftoff!") → 5 4 3 2 1 then Liftoff!. ✓
Part 4 trace table (run-verified):
| # | Loop | Actual output | # of runs |
|---|---|---|---|
| 1 | i=1; while i<=3: print(i); i=i+1 |
1 2 3 |
3 |
| 2 | n=3; while n>0: print(n); n=n-1 |
3 2 1 |
3 |
| 3 | total=0; i=1; while i<=4: total+=i; i+=1; print(total) |
10 |
4 (loop) |
| 4 | c=0; while c<=3: c+=1; print(c) |
4 |
4 |
| 5 | i=10; while i>0: print(i); i=i-3 |
10 7 4 1 |
4 |
| 6 | total=0; k=0; while True: k+=1; total+=k; if total>=20: break; print(k,total) |
6 21 |
6 |
(Notes: #3 prints only the final total 10 (1+2+3+4) after looping 4 times. #4's c ends at 4 because <= 3 lets it step from 3 to 4 before re-checking — the signature off-by-one. #5 steps down by 3: 10, 7, 4, 1; from 1, 1 - 3 = -2, and -2 > 0 is False, so it stops after 4 prints. #6's running total reaches 21 (1+2+3+4+5+6) on the 6th pass — the first time it's ≥ 20 — then breaks.)
Part 5 bugs (run-verified where runnable):
- Bug A count = 1 / while count <= 3: / print(count) (no step) → (i) infinite loop; (ii) the counter is never updated, so count <= 3 stays True forever and it prints 1 endlessly (the missing STEP); (iii) fix — add the counter update inside the loop:
python
count = 1
while count <= 3:
print(count)
count = count + 1
Output (run-verified): 1, 2, 3. (Bug A is read, not run; only this fixed form is run.)
- Bug B while i < n with n = 5 summing → (i) off-by-one; (ii) i < 5 stops at 4, so it sums 1+2+3+4 = 10 (run-verified) instead of the intended 15 — the < excludes the boundary 5; (iii) fix — use <= n:
python
n = 5
total = 0
i = 1
while i <= n:
total = total + i
i = i + 1
print(total)
Output (run-verified): 15. (while i < 6: also works.)
Part 6 (expected): (1) most commonly #4 (c ends at 4, not 3) or a boundary miscount in #5/#6. (2) The loop body runs with c at 3 (3 <= 3 is True), does c += 1 to make it 4, THEN checks 4 <= 3 → False and stops; so c ends one past the boundary. (3) A counter's printed values are what you print inside the loop (e.g., 1, 2, 3); the counter's final value is what it holds after the loop ends (one past the last that passed the condition). (4) Without a STEP, the condition never moves toward False, so it's always True and the loop repeats forever. (5) < n excludes n (stops one short); <= n includes n (one more iteration).
Part 7 (AI-critique): full credit for a specific catch — most commonly the AI claiming i ends at 5 after a while i <= 5 loop (it ends at 6), or miscounting loop #2's iterations as 5 or 11 instead of 6. Full credit also if the student verified each AI claim by running it and counting the lines.
Grading rubric — 50 points
| Criterion | Full | Partial | None |
|---|---|---|---|
| Part 3 — wrote & ran 3 loops (counter, accumulator → 55, countdown; code + output shown) (14) | 14 | 7–11 | 0–5 |
| Part 4 — trace table (predictions attempted + all 6 actual outputs/counts correct from running) (14) | 14 | 7–11 | 0–5 |
Part 5 — found & fixed both bugs (infinite loop: missing step; off-by-one: <→<=; fix for each) (12) |
12 | 6–10 | 0–4 |
Part 6 — analysis (step-then-recheck → ends one past; counter values vs. final value; < vs <=) (6) |
6 | 3–5 | 0–2 |
| Part 7 — AI-critique (names a specific miscount/off-by-one caught by running & counting) (4) | 4 | 2 | 0–1 |
Quality gate (self-checked): every model output above (Part 3: 1–10, 55, 5 4 3 2 1 + Liftoff!; Part 4: 1 2 3/3, 3 2 1/3, 10/4, 4/4, 10 7 4 1/4, 6 21/6; Part 5 fixed forms: 1 2 3 and 15) was produced by actually running the code in Python — execution gate: PASS. No output is hand-traced. The one infinite loop (Bug A) is presented as code to read, never run unbounded; only its fixed form is run. The lab grades the student's process (write → trace/count → debug), not a single fixed wording for the open-ended Part 3.
~ Prof. Okafor's edition · Fall 2026 · built with thecoursemaker.com