Choose the loop from the brief
Look for the source of repetition. “Read exactly five results” gives you a count before you start, so a for loop with range(5) is a natural choice. “Read until the user enters −1” gives you a stopping condition, so a while loop is clearer. “Visit each result in this list” usually calls for for result in results. Each construct can sometimes solve the same problem, but picking the one that matches the brief helps the reader understand the intention.
range(5) gives 0, 1, 2, 3, 4. The loop does five passes even though the last index is 4. range(1, 6) gives 1, 2, 3, 4, 5. Use it if the number itself appears in a label such as Day 1. If the loop variable is never used, _ is a common name. range(2, 8, 2) gives 2, 4, 6 because the third argument changes the step. Do a quick trace whenever the start or step changes.
Trace the condition, the update and the state
count = 0
total = 0
rating = int(input())
while rating != -1:
if 1 <= rating <= 5:
count = count + 1
total = total + rating
rating = int(input())
print(count, total)
| Value read | rating != -1 | In range? | count after processing | total after processing |
|---|---|---|---|---|
| 5 | True | Yes | 1 | 5 |
| 8 | True | No | 1 | 5 |
| 2 | True | Yes | 2 | 7 |
| −1 | False | Body skipped | 2 | 7 |
The first input() establishes an initial value before the condition is checked. The second is inside the loop but outside the if, so even a rejected rating leads to the next read. The sentinel controls when the loop ends and is never counted or added. The final print is outside the loop because the brief needs one result after all the data. If the first value is −1, the body never runs and both totals stay zero. This matters because you cannot assume that a condition-controlled loop will run at least once.
Diagnose three common faults
- A total that equals only the last valid amount usually means the accumulator was reset inside the loop or assigned the latest value instead of being updated. Check its initialisation and update separately.
- Output repeated after every amount usually means
printis inside the loop. If the brief wants a final total, move it out. Keep debug prints temporarily if they help you trace, then remove them before checking exact output. - A loop that stalls on an invalid value usually lacks an update on that path. Try one invalid value followed by the sentinel. If it keeps running, stop the sandbox and mark every route through the body that reaches the update. If the sandbox reports
EOFErrorinstead, count the lines supplied and compare them with the calls toinput().
Practise a Paper 2 change request
A school tracks completed reading sessions. The first line is the number of days, followed by one value per day. A day counts as complete when the value is at least 20. Output the number of complete days and the total minutes across those days on separate lines. Use the sandbox to create a program for this change. Start by writing a trace for five days with 19, 20, 0, 30 and 20. Then test no days, all days complete and all days below the threshold. Pick a name for each variable that says what it measures.
Read nested repetition from the inside out
If a class has three teams and each team submits two scores, the inner loop reads two scores each time the outer loop advances. There are six reads in total. Put a team subtotal inside the outer loop but before the inner loop, so it resets once per team. Put the grand total before both loops, so it keeps the contributions from all teams. Trace two teams with different scores to check that the two totals have the right lifetime.