Bug hunt

Side Quest: The Extra Turn

Games often have a turn limit: "you get 3 tries". Counting turns is also a classic place for a sneaky bug. The loop runs one time too many (or one too few). Programmers call this an off-by-one error.

Count the rounds by hand

Here the counter starts at 0 and goes up by 1 each round:

turn = 0
while turn < 3:
    turn += 1
    print("Turn", turn)

At each check, turn is 0, then 1, then 2: three rounds. When it reaches 3, 3 < 3 is False and the loop stops. With turn <= 3, the check passes at 3 as well, so a fourth round sneaks in.

Pip tip: Test the smallest case. With a limit of 1, does your loop run exactly once?

The bug

Pip must cross the Rope Bridge before the tide comes in. The rules say Pip gets 3 turns and moves 4 metres per turn. The bridge is 15 metres long, so Pip should not make it. But the game log says:

Turn 1: 4 m
Turn 2: 8 m
Turn 3: 12 m
Turn 4: 16 m
Across!
Turns used: 4

Assignment

  1. Fix the loop so Pip gets exactly max_turns turns.
  2. Keep the break that stops early once Pip is across.
  3. With the starting values, the log should end with The tide wins! and Turns used: 3.