Search HoldMyTwist

Browse all posts

Episode 10

The Drummer Who Missed a Beat

Duck needs four drumbeats during parade rehearsal. Robot's last counter shows three.

Episodes

Computing Basics

    Robot sits at a drum in the workshop. Duck conducts from inside the monitor.
    Robot raises a drumstick and points to the small program card.
    The Python-style program repeats one tap for every item in range(4). Its counter values will be 0, 1, 2 and 3.
    Duck holds up three feather tips and looks doubtful. No tap has happened.
    Robot presses the start control once to begin the counted loop.
    At counter value 0, Robot strikes the drum once. The total number of taps is now one.
    At counter value 1, Robot strikes the drum once again. The total is now two taps.
    At counter value 2, Robot strikes the third tap.
    At counter value 3, Robot makes the fourth tap. Duck counts it with a wing.
    The range has no further value. Robot sets the sticks down and the drum is silent.
    Four aqua markers stand in a row beside Robot and Duck. The trace pairs indices zero through three with taps one through four.
    Robot places the final counter label 3 beside the completed total of four taps.
    Duck proposes changing the loop body while keeping the same four counter values.
    Robot writes two tap actions inside the body. The loop has not started again.
    Duck pauses with its conducting wing raised. The new two-tap body and four-turn loop are visible.
    Robot performs four pairs of taps, giving eight taps in total. The counter values are still 0, 1, 2 and 3.
    Robot sets the repeat count to zero for a fresh run. The sticks stay flat on the silent drum.
    There are no counter values in range(0), so the loop body never runs and no tap is made.
    Duck bows after the finished rehearsal while Robot holds the four counter tokens.
    Robot packs the drum on the parade trolley. Duck watches from the monitor on the trolley.

    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.

    Python
    for i in range(4):
        print(i, "tap")

    Two outputs per iteration

    Python
    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

    Python
    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)