Loop Conditions and Off-by-One Errors
The program below is meant to count the treadmill laps a runner needs to cover a distance typed at the prompt. Each lap adds 0.4 km. Each input traced below exposes a different fault in the loop.
target = input("Target distance in km: ")
distance = 0
laps = 0
while distance != target:
distance = distance + 0.4
laps = laps + 1
print("Laps:", laps)When the condition is checked
The runner types 3. Since
input() always returns a string, target holds the text "3". At every check distance is a number and target is a string, so distance == target is False, distance != target is True, and the loop never ends.Here no pass can make it False. Writing
distance < target fails another way: the first check, 0 < "3", raises TypeError: '<' not supported between instances of 'int' and 'str'. The repair converts the input on the first line: target = float(input("Target distance in km: ")).Comparing floats
With 3 typed again,
target is 3.0. After seven laps distance is 2.8, and after eight it's 3.1999999999999997. The body jumps past 3 without landing on it, so the loop still never ends. Even 1.2, which three laps reach, fails, because the third lap gives 1.2000000000000002.With
distance < target as the condition, an input of 3 gives Laps: 8, since seven laps make only 2.8 km, and 1.2 gives Laps: 3.Testing at the boundaries
An off-by-one error shows at the inputs where the lap count changes. Eight laps make exactly 3.2 km, so an input of 3.2 should give
Laps: 8. It gives Laps: 9, because eight additions of 0.4 make 3.1999999999999997, which is less than 3.2.The program below is the lap counter with the two repairs so far: the input is converted with
float, and the loop runs while distance < target. It also prints the distance after every lap. Run it and type 3, then try these:• Run it again and type 3.2, a target that eight laps should reach exactly.
• Run it again and type 1.2, and read the distance after the third lap.
• Change
< in the while line to !=, run it, and type 1.2. The distance never equals 1.2 exactly, so the loop never ends. Nothing appears while it runs, and after a few seconds the run stops with the message "Your program ran too long". Press Stop to end it sooner.target = float(input("Target distance in km: "))
distance = 0
laps = 0
while distance < target:
distance = distance + 0.4
laps = laps + 1
print("After lap", laps, "the distance is", distance)
print("Laps:", laps)
Output
Press Run to see what this program prints.
Note: This IDE is for experimenting with the code in this lesson. Your changes are not saved, and refreshing the page puts back the original lesson code.
Arithmetic on whole numbers is exact, so the repair counts meters, with
round turning the float product into a whole number.target = float(input("Target distance in km: "))
goal = round(target * 1000)
meters = 0
laps = 0
while meters < goal:
meters = meters + 400
laps = laps + 1
print("Laps:", laps)Now 3.2 gives
Laps: 8, and 3.3, just past it, gives Laps: 9. The smallest target that makes sense, 0, gives Laps: 0, since the condition is False at the first check.One pass too soon
An interval session runs several repeats, each with its own target, and counting its laps takes a loop inside a loop.
reps = int(input("Repeats: "))
laps = 0
meters = 0
rep = 1
while rep < reps:
target = float(input("Target distance in km: "))
goal = round(target * 1000)
while meters < goal:
meters = meters + 400
laps = laps + 1
rep = rep + 1
print("Laps:", laps)
Output
Press Run to see what this program prints.
Note: This IDE is for experimenting with the code in this lesson. Your changes are not saved, and refreshing the page puts back the original lesson code.
Given 3 repeats, of 2, 1.2, and 0.8 km, it should print
Laps: 10 (5, 3, and 2). It asks for only two targets and prints Laps: 5. Run the program above and type 3, 2, and 1.2 to see it. As the lesson finds each mistake, repair it in the program and run it again with all three targets.Since
rep starts at 1, rep < reps turns False when rep reaches 3, so the outer loop runs only twice. That comparison is one short: rep <= reps was meant. The second repeat adds no laps either. The line meters = 0 runs once, before the outer loop, so meters is still 2000 when the goal becomes 1200. Placed inside the outer loop, it resets for every repeat. With both repairs the program prints Laps: 10.Flags
A loop can also be driven by a flag, a variable that holds True or False, instead of a comparison in the while line. The loop of the meters version becomes:
running = True
while running:
meters = meters + 400
laps = laps + 1
if meters >= goal:
running = False
An input of 3.2 still gives
Laps: 8, but the boundary test at 0 gives Laps: 1. The goal is compared only after a lap has been added, so the first lap always runs. Setting running = meters < goal before the loop repairs it.