← Back to the Introduction to Computer Science outline The Course Maker
← Introduction to Computer Science outline
Week 5 · Coding Lab

Week 5 Coding Lab: Counting Loops for a Campus Recycling Drive

Introduction to Computer Science Free sample edition

Part 1 — The Big Picture

This week you learned that a while loop repeats its body for as long as its condition is True, that the condition is tested only at the top of each pass, that a counter ends one past the last value that passed, and that a loop with no update never stops. In this lab you put those ideas to work on a campus recycling drive that collects 12 cans per pickup trip toward a goal of 50 cans. You predict what six short loops print, run them, and study every gap between your prediction and the run. Then you read three broken programs, fix each with the smallest change that works, write a small loop of your own, and check a chatbot's answers to the same questions by running the code, because a chatbot predicts likely text and does not run your program.

Time budget: Plan for about 90 minutes.

Part 2 — The Task

Guiding question: When the recycling drive collects 12 cans per trip toward a goal of 50, which of these six predictions will you get wrong, and how will each run show you where the loop really stops and why?

Workshop data: A campus recycling drive: six short loops and three broken ones

Source: Six short Python loops and three broken programs about an illustrative campus recycling drive that collects 12 cans per pickup trip toward a goal of 50 cans, written for this lab and printed in full in Part 4; the drive and its numbers are invented and describe no real campus or person.

  • Rows: 9 short programs in all: 6 loops to trace in the first table and 3 broken programs in the second table
  • Language: Python 3, run in a free browser runner; no file to download and nothing to install
  • Context: an illustrative recycling drive counting cans and pickup trips

Part 3 — Materials & Procedure

Tools and materials

  • A free browser-based Python runner (an online editor that runs Python 3 with no account and nothing to install), Python Tutor at pythontutor.com for step-by-step visualization, the official Python documentation at docs.python.org, and one approved chatbot used as a pair-programmer whose code the student runs before trusting

Ways of working

  • write-run-and-verify
  • trace-and-predict-output
  • find-and-fix-the-bug

Procedure

  1. Open a free browser Python runner in one tab and Python Tutor at pythontutor.com in another. In the runner, delete any sample code so the editor is blank, and find its Stop button before you run anything. Tool: A free browser-based Python runner (an online editor that runs Python 3 with no account and nothing to install), Python Tutor at pythontutor.com for step-by-step visualization, the official Python documentation at docs.python.org, and one approved chatbot used as a pair-programmer whose code the student runs before trusting. Check: An empty editor with a Run button, a Stop button, and a separate output panel.
  2. In the first table in Part 4, write your prediction for each of the six loops before you run anything. Then type each program into the runner exactly as printed, with four spaces in front of every line of the loop body, press Run, and copy the output into the Actual output column. Tool: A free browser-based Python runner (an online editor that runs Python 3 with no account and nothing to install), Python Tutor at pythontutor.com for step-by-step visualization, the official Python documentation at docs.python.org, and one approved chatbot used as a pair-programmer whose code the student runs before trusting. Check: Six actual outputs, one per row, each copied character for character from the output panel; at least one is likely to differ from your prediction.
  3. Type each broken program from the second table into the runner and press Run. If a program keeps printing and never finishes, press Stop. For an error, scroll to the bottom of the error message and copy the error type from its last line. For a program that runs, compare what it printed with what its description says it should print. Tool: A free browser-based Python runner (an online editor that runs Python 3 with no account and nothing to install), Python Tutor at pythontutor.com for step-by-step visualization, the official Python documentation at docs.python.org, and one approved chatbot used as a pair-programmer whose code the student runs before trusting. Check: The second table shows what each broken program printed, or the error type on its last line; one program prints one line too few, one never stops until you press Stop, and one stops before anything runs.
  4. Fix each broken program with the smallest change that makes it do what its description in Part 4 says, run the fixed version, and record the new output. Tool: A free browser-based Python runner (an online editor that runs Python 3 with no account and nothing to install), Python Tutor at pythontutor.com for step-by-step visualization, the official Python documentation at docs.python.org, and one approved chatbot used as a pair-programmer whose code the student runs before trusting. Check: Each fixed program runs to the end without an error and prints the trip numbers, can counts, or message its description asks for.
  5. Paste the first loop from the first table into Python Tutor, click the button to visualize it, and step forward one line at a time. Watch the arrow jump from the last body line back up to the while line, and write down the value of cans each time the arrow reaches the while line. Tool: A free browser-based Python runner (an online editor that runs Python 3 with no account and nothing to install), Python Tutor at pythontutor.com for step-by-step visualization, the official Python documentation at docs.python.org, and one approved chatbot used as a pair-programmer whose code the student runs before trusting. Check: Python Tutor shows cans at 0, 12, 24, 36, 48, and 60 at the while line, and the loop ends only when the test is checked at 60, which matches what the runner showed.
  6. Write your own loop: start minutes at 0 and sessions at 0, and while minutes is less than 100, add 15 to minutes and 1 to sessions; after the loop, print sessions and minutes on one line. Predict its output, run it, and fill in the third table. Tool: A free browser-based Python runner (an online editor that runs Python 3 with no account and nothing to install), Python Tutor at pythontutor.com for step-by-step visualization, the official Python documentation at docs.python.org, and one approved chatbot used as a pair-programmer whose code the student runs before trusting. Check: A loop that runs to the end without an error, with your predicted and actual output side by side.
  7. Open your approved chatbot, paste the critique prompt from Part 6 as one message, then check each of its claims by running the code it talks about. If you are using the equivalent route, mark up the printed flawed analysis in the same way. Tool: A free browser-based Python runner (an online editor that runs Python 3 with no account and nothing to install), Python Tutor at pythontutor.com for step-by-step visualization, the official Python documentation at docs.python.org, and one approved chatbot used as a pair-programmer whose code the student runs before trusting. Check: The fourth table lists each chatbot claim, what your run showed, and a correction wherever the two disagree.

Fallback route

When to use it: Use this route if your browser Python runner will not open or will not run code.

Run every program in Python Tutor instead, which also runs Python 3 in the browser: paste each program, click the button to visualize it, and step to the end to read its output or its error. For the program that never stops, step forward until the same line has printed several times, then stop stepping and record what you saw. Complete every table, analysis question, and the critique step, and submit the same items for equal credit.

This route earns the same credit as the primary route.

Part 4 — The Scaffold

Predict first and run second, so the gap between the two columns is your own evidence of where a loop really stops.

Student scaffold table 1
Loop (each one starts fresh) Your prediction Actual output from the run Same or different?
Loop 1: cans = 0, trips = 0, while cans < 50: with body cans += 12 and trips += 1, then print(trips, cans)
Loop 2: cans = 0, while cans <= 48: with body cans += 12, then print(cans)
Loop 3: trip = 1, while trip < 4: with body print(trip) and trip += 1
Loop 4: trip = 1, while trip <= 4: with body trip += 1, then print(trip)
Loop 5: total = 0, bag = 1, while bag <= 3: with body total += 12 and bag += 1, then print(total, bag)
Loop 6: n = 1, while True: with body if n * 12 > 50: then print(n) and break, else n += 1

Read each broken program from its run before you change a single character.

Student scaffold table 2
Broken program and what it should do What printed, or the error type on the last line Your one-change fix Output after the fix
Bug 1, should print the trip numbers 1, 2, 3, and 4: trip = 1, while trip < 4: with body print(trip) and trip += 1
Bug 2, should print the can count before each trip and stop once it reaches at least 50: cans = 0, while cans < 50: with body print(cans)
Bug 3, should add 12 cans per trip until at least 50 and then print Goal reached: cans = 0, while cans < 50 with body cans += 12, then print("Goal reached")

Write your own small loop, then let the run confirm or correct what you expected it to print.

Student scaffold table 3
Line you wrote Output you expected Output of the run
Line 1
Line 2
Line 3
Line 4
Line 5
Line 6

Check every chatbot claim against a run so the run, not the chatbot, has the last word.

Student scaffold table 4
Chatbot claim, quoted What your run showed Correct as stated, or your correction

Part 5 — Analysis Questions

  1. Which of your six predictions in the first table differed from the run? For each one, write a pass-by-pass trace that reaches the actual output, with the value tested at the top of each pass, and name the rule that explains the gap.
  2. For Bug 2 and Bug 3, what did each program do when you ran it: what printed, how it ended, and, for an error, which type the last line named? Explain why one of them is a syntax error and the other is a logic error.
  3. Bug 1 runs without any error message but prints one line too few. Explain why that makes it an off-by-one logic error, and why changing the start to 0 would also print four lines but would not fix it.
  4. Choose one: (a) describe how you would decide whether to trust a chatbot's claim about where a loop stops, using one claim from your critique as the example; or (b) describe how writing the test at the top of each pass changed what you noticed about the recycling loops, using one row of your first table as the example.
  5. Every loop in this lab used small whole numbers, such as 12 cans a trip and a goal of 50. Name one thing these runs could not tell you about a loop whose step or starting value changes, and say how you would check it.

Part 6 — AI-Critique Moment

Choose one equal-credit critique route. Route A: ask an approved AI tool to critique your completed work, then check its response against your own evidence. Route B is the no-AI equivalent printed below.

Approved tools: Claude, Gemini, ChatGPT, Grok, Microsoft Copilot.

Prompt to paste:

I'm doing a beginner Python lab about a campus recycling drive, written for a class activity. Each pickup trip collects 12 cans and the goal is 50 cans. Please do six things, and keep each answer short. First, predict exactly what each of these programs prints, and explain your reasoning for the first two. Program A: cans = 0, trips = 0, then while cans < 50: with the body cans += 12 and trips += 1, then print(trips, cans). Program B: cans = 0, then while cans <= 48: with the body cans += 12, then print(cans). Program C: trip = 1, then while trip <= 4: with the body trip += 1, then print(trip). Second, this program should print the trip numbers 1, 2, 3, and 4 but prints only three of them; fix it and show your fixed version: trip = 1, then while trip < 4: with the body print(trip) and trip += 1. Third, this program should print the can count before each trip and stop once the count reaches at least 50, but it never stops; fix it: cans = 0, then while cans < 50: with the body print(cans). Fourth, does Python have an until loop, so that I could write until cans >= 50: instead? Fifth, write a short program that counts how many trips it takes to reach at least 50 cans at 12 cans a trip. Sixth, does a while loop always run its body at least once?

Test the response for these likely failures:

  • Chatbots often predict that Program A prints 4 48 or 5 50, as if the loop stopped exactly on the goal of 50 cans.
  • A chatbot may give 60 for Program B while explaining that the loop stops when cans reaches 48, so its explanation and the printed value disagree.
  • A chatbot may say Program C prints 4, the last number in its condition.
  • A chatbot may fix the trip-number program by starting at trip = 0, which runs without an error and prints four lines.
  • A chatbot may add the missing line cans += 12 with no indentation, below the loop, and call the program fixed without running it.
  • A chatbot may describe an until loop as if Python had one and show until cans >= 50: as working code.
  • A chatbot may claim that a while loop always runs its body at least once, so its trip counter can never report zero trips.

Write 2-3 sentences that name one thing the chatbot got right and at least one claim you corrected, and for each correction give the output or error type your own run showed.

Equivalent route (no AI required)

No AI account is required for this lab. Instead of the chatbot, critique this flawed analysis, printed here in full: “Program A prints 4 48, because the loop stops as soon as it reaches the goal of 50. Program B prints 60, because the loop stops when cans reaches 48. Program C prints 4, the last number in its condition. To fix the trip-number program, start at trip = 0; this version is correct. To fix the program that never stops, add the line cans += 12 below the loop with no indentation; this version is working. Python also has an until loop, so until cans >= 50: works too. A while loop always runs its body at least once, so the trip counter can never report zero trips.” Mark it up: quote each flawed sentence, run the code it describes in your browser Python runner, and write the corrected claim beside it with the run's output or error type as evidence, including a run of Program A with cans = 60 at the start. This route assesses the same objectives as the chatbot critique, namely tracing a while loop, predicting its counter's final value, and repairing an off-by-one error and an infinite loop by running the code (Objective 4 and Objective 3), and it takes comparable effort, about 20 minutes of the lab. It is scored by the same rubric for the same credit, and its marked-up analysis replaces the chatbot report and share link in your submission. You do not need to say which route you chose.

Part 7 — What to Submit

  • All four completed scaffold tables
  • Written answers to all five analysis questions
  • Your minutes-and-sessions loop with its run output
  • The chatbot critique report with its share link, or the equivalent route's marked-up flawed analysis

Deliverable:

A short Coding Lab report on an illustrative campus recycling drive: four completed tables (six predictions beside their runs, three broken programs read and fixed, your own minutes-and-sessions loop, and the checked chatbot claims), written answers to five analysis questions, and a critique of a chatbot's answers to the same questions, or, on the equivalent route, a marked-up correction of the printed flawed analysis. Submit in any accessible file type or as a link; a recorded response may be submitted as audio or video with a transcript, or as text. Choose a written, recorded, or visual form for your answers, such as screen captures of your runs with a short note for each; every form is graded by the same rubric.

Rubric

The rubric below shows how each submitted part is evaluated; bands are full, partial, or none.

Workshop evaluation criteria
Criterion Full Partial None
Predicting and running the six loops All six predictions are recorded before the runs, and all six actual outputs match what the programs print, copied character for character. The table is mostly complete, but an actual output is missing or miscopied, or predictions appear to have been filled in after running. No predictions or runs are recorded, or the outputs are invented rather than run.
Explaining the gaps between prediction and run Every prediction that differed from its run is traced pass by pass to the actual output, with the value tested at the top of each pass and the governing rule named, such as the top-of-pass test or the counter ending one past the last value that passed. The gaps are listed, but a pass-by-pass trace is missing or the rule behind a gap is not named. No gap is explained, or the response blames the runner instead of the rule.
Finding and fixing the three bugs Each bug is read correctly from its run: what printed, how it ended, or the error type, a one-change fix, and the fixed program's output, with Bug 1 named as an off-by-one logic error, Bug 2 stopped by hand and repaired with an indented update line, and Bug 3 named as a syntax error. Two of the three bugs are read and fixed correctly, or all three are fixed but an error type or the run evidence is missing. The bugs are not attempted, or errors are named without running the programs.
Writing, running, and verifying a loop The minutes-and-sessions loop runs to the end, starts both names before the loop, updates both inside the body, and its expected and actual outputs are recorded side by side. The loop runs but misses one requirement, or the expected or actual output is not recorded. No loop is submitted, or it was not run.
Critiquing the AI analysis or the printed flawed analysis The chatbot's answers, or on the equivalent route the printed flawed analysis, are quoted and at least two claims are corrected with evidence from the student's own runs. The critique names errors but corrects only one, or corrects without run evidence, on either route. No critique is submitted on either route, or chatbot or flawed text is pasted without any checking.
Reflection and limitation The chosen reflection option is answered with a specific example from this lab, and the limitation answer names something these runs cannot show and a sensible way to check it. The reflection or the limitation is general, without an example from this lab or a way to check it. The reflection and limitation questions are not answered.

Quality Gate

  • Every prediction in the first table was written before its program was run.
  • Every actual output and error type is copied from your own run, never from memory or a chatbot.
  • Each error is named by its type from the last line of the error message, not by the wording after the colon.
  • Each fix changes as little as possible and its fixed program was run to the end.
  • No output, count, or claim is invented anywhere in the submission.
  • The critique quotes the chatbot or the flawed analysis and corrects at least two claims with evidence from your runs.

Built with The Course Maker · samples.thecoursemaker.com