Episode 10
The Drummer Who Missed a Beat
Duck needs four drumbeats during parade rehearsal. Robot's last counter shows three.




















Episode transcript
Four taps for rehearsal
Robot is at the drum, sitting in the workshop. Duck is inside the monitor conducting.
Duck: Four taps for our parade rehearsal
Robot holds a drumstick high, pointing at the little program card.
Robot: Tap once every loop.
range(4): 0, 1, 2, 3
STORY BODY: ONE DRUM TAP PER TURN
Duck: The last counter is three?
Duck raises 3 feather points up. He looks unsure.
Duck: I think we'll be one tap short
Begin at zero
START ONCE
Robot: Let us count what actually happens.
When counter = 0 then robot hits the drum once. So now there is 1 hit.
COUNTER0 → TAP1
Robot: Counter at zero. First hit
At counter 1 Robot once again hits the drum. That's two hits in total.
COUNTER1 → TAP2
Duck: Two.
COUNTER2 → TAP3
Robot: Counter two. Third tap.
The fourth tap
At counter 3 Robot strikes a fourth time. Duck keeps track with a wing.
COUNTER3 → TAP4
Duck: Here's our fourth!
NO NEXT VALUE → STOP
Robot: No more values. Loop stops.
COUNTERS: 0, 1, 2, 3. TAPS: 4
FINAL COUNTER LABEL: 3. ITERATIONS: 4
Robot: The last label is not the count of taps.
Change the repeated body
Duck suggests a different loop body while still using the same four counters.
Duck: Now two taps each time
SAME FOUR TURNS. TWO TAPS PER TURN
The counter is still 0,1,2,3.
4 PAIRS = 8 TAPS
Robot: Eight. Four pairs.
No turns and an apology
Robot sets the number of repeat cycles to 0 for a new start. The sticks are just lying flat on the silent drum.
range(0): NO VALUES
Duck: And what happens if we don't turn?
There are no counter values to count in range(0), so the loop body doesn't execute. No tap is made.
NO ITERATIONS → NO TAPS
Robot: No turns means no taps.
The four counter tokens are in Robot's hand.
Duck: I'm sorry, zero.
Robot: save it a seat in the band.
Counted loops
A counted loop goes through all values in the sequence. The body of the loop runs once for each value. For example, in Python, range(4) gives us 0, 1, 2 and 3. There are 4 values. Endpoint is not included (which is four). So if you do one tap each time then you do 4 taps. Now change the body to two taps then you'll do 8 taps.
Last counter label is 3. Number of iterations done is 4.
Run a Python example
The runnable Python code prints both counter and word tap for each iteration. So for each line that gets printed, it's one drum tap in the story. It does not actually produce sound or control a drum.
for i in range(4):
print(i, "tap")Two outputs per iteration
for i in range(4):
print(i, "tap")
print(i, "tap")The for line in Python causes the indented lines below the for line to be run for every number in the range. The counter is 0 on Duck's first turn. Python processes the first print, then the second print. They both print out 0 tap. Then the counter increments to 1.
No iterations
for i in range(0):
print(i, "tap")The zero iteration version just won't print at all.
Try three turns
THREE ITERATIONS. TWO TAPS IN EACH BODY.
Answer
range(3): 0, 1, 2. TAPS: 2 + 2 + 2 = 6
6 taps in total. range(0) gives you no iterations, no taps.
STOP WHEN THE RANGE HAS NO NEXT VALUE
Sources
Python documentation, More Control Flow Tools: the range() function (opens in a new tab)