Week 5 — Loops That Count, Total, and Stop — Traditional alternative (no AI)
Instructors: publish this instead of the adaptive version if you prefer, and unpublish or delete the adaptive one.
Assignment 5 of the term — trace a while loop pass by pass, find where its counter and its total end, and explain an overshoot to someone who has never programmed.
This week you learned that a while loop tests its condition only at the top of each pass, kept counters and running totals, compared < with <=, and stopped a loop that never ends. In this assignment you trace four short problems, run each one to check your prediction, run two changed versions that show an off-by-one error and an infinite loop, and explain a real surprise to someone who has never programmed. This builds on Week 4's comparisons. This week's lecture tutorial, quiz, discussion, and Coding Lab practise the same ideas from other angles.
The task
A friend is using a reading-challenge app that runs the course's reading log: it starts at 45 pages, aims for 300, and adds 30 pages a day. The app reports 9 days and 315 pages, and your friend says, "The goal was 300. The program must be broken, because it added too much." Your job is to check what Python really did, the way a programmer would: predict each value before you run it, run the code in a free browser Python runner, and explain what happened. All four problems are illustrative and use this week's loops.
Work these four problems. For each one, write your prediction first, then run the code and write the actual output beside your prediction:
- The reading log starts with
pages_read = 45andgoal = 300. How many pages are still needed before the loop starts? - The reading log's loop
while pages_read < goal:adds 30 pages on each pass and makes 9 passes, starting from 45 pages. What doesprint(pages_read)show after the loop? - A counter starts at
count = 1, and the loopwhile count <= 5:adds 1 tocounton each pass. What doesprint(count)show after the loop? - A total starts at
total = 0and a counter atn = 1; the loopwhile n <= 5:runstotal += nand thenn += 1on each pass. What doesprint(total)show after the loop?
Beside each answer, add one brief interpretation:
- Problem 1: work out how many passes the loop needs at 30 pages a pass, starting from the division of the pages still needed by 30, and say what
pages_read < goaltests at the top of each pass. - Problem 2: explain why the loop ends at 315 rather than 300 by tracing the test at the top of the ninth pass, and name the rule that explains it.
- Problem 3: say how many passes the loop makes and why the counter ends where it does. Then change
<=to<, run it again, and report what it prints. - Problem 4: name which variable is the accumulator and which is the counter. Then delete the line
n += 1, run the program briefly, stop it with the Stop button or Ctrl+C, and explain what you saw.
Explain it to a non-programmer
Write a 3–5 sentence message to your friend that explains, in everyday words, why the app reports 315 pages instead of stopping at 300, using the idea that the program checks the goal only before each day's reading, never partway through one. Name one limit or caveat, such as a situation where going past a limit would matter more than it does for reading. Explain any technical term in everyday words. More than one good message is possible.
For a different, ungraded example, imagine a teammate whose kitchen-timer program was supposed to show 3, 2, 1 and then Ready, but showed only 3, 2, and Ready. Model message: "The timer keeps counting down while the seconds left are greater than 1, so when it reaches 1 the check says stop before the 1 is ever shown. Changing that check to greater than 0 lets the last second through. The program did exactly what it was told; the rule was off by one. One catch: a quick test that shows the last number is the only way to be sure, because the program gives no error message for this kind of mistake."
Use that pattern for your friend's question, with your own wording and evidence.
Show your traces. A reader should be able to follow every prediction pass by pass and see the actual output of your run beside it, without guessing which values or tests you used.
Deliverables
Submit a single report containing:
- the four answers, each with your prediction, the step-by-step trace, the actual output of your run, and the brief interpretation the task names for that problem;
- the runs of the Problem 3 loop with
<in place of<=and of the Problem 4 loop withoutn += 1, each with what printed and how you stopped it if it did not stop on its own; and - your message to your friend, explaining why the app went past 300 and naming one limit.
Keep it to roughly one page. Clear and correct beats long.
Choose how you show it. Present your report as written text, or as a recorded audio or video explanation of about three minutes with a transcript, or as labelled screen captures of your runs with a short note for each. Every form is graded by the same scoring criteria. 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.
How you'll be graded
The full analytic rubric is attached to this assignment in Canvas.
Academic integrity and AI use
This version is designed to be completed without AI: make the predictions, run the code, and write the interpretations and the message yourself, without any AI tool. You may use a free browser Python runner, Python Tutor, and the official Python documentation. Submitting AI-generated work as your own, or copying another student's work, is an integrity violation under the course policy.
Built with The Course Maker · samples.thecoursemaker.com