GoalFind the smallest or largest value with a loop, and sort loop errors into syntax, run-time and logic errors.
New words
syntax error · a mistake in the rules of the language; Python refuses to start the programrun-time error · a mistake that makes the program stop while it is runninglogic error · a mistake that lets the program run but gives a wrong result
Explanation
To find the smallest value, keep a variable for the best one so far. Give it a starting value that is bigger than any real value, such as 999. Inside the loop, an IF compares each new value with the best so far, and a smaller value replaces it. The IF is indented inside the for, so its body is indented twice. A second variable can store the position of the winner; the counter plus 1 gives the lap number. When a program does not work, find out which of three kinds of error it has. A syntax error stops Python before the first line runs and the message names the line. A run-time error stops it while it runs. A logic error does not stop it, so you only find it by checking the output against what you expected.
Python
1laps = int(input("How many laps? "))
2best = 9993bestLap = 04for lap inrange(laps):
5 time = int(input("Time in seconds? "))
6if time < best:
7 best = time
8 bestLap = lap + 19print("Fastest: lap", bestLap, "in", best, "seconds")
Output
Fastest: lap 4 in 57 seconds
Worked examples
lap
time typed
time < best?
best
bestLap
(before)
999
0
0
62
yes
62
1
1
58
yes
58
2
2
65
no
58
2
3
57
yes
57
4
4
60
no
57
4
The lap-time program from above for 5 laps. The user types the times 62, 58, 65, 57 and 60. The lap column of the table is the counter (0 to 4), and bestLap is lap + 1. The table shows best and bestLap after each pass. Lap 3 (65) and lap 5 (60) are not below the best so far, so nothing changes. The output is Fastest: lap 4 in 57 seconds.
Python
1for i inrange(3):
2print(i)
Output
IndentationError: expected an indented block after 'for' statement on line 1
A syntax error. The line print(i) is not indented, so the loop has no body. Python refuses to run the program and the last line of its message names the problem. No output appears.
Program
What happens
Kind
n = input() … for i in range(n):
TypeError: 'str' object cannot be interpreted as an integer
run-time
for i in range(3): print(j)
NameError: name 'j' is not defined
run-time
for i in range(1, 5): (meant 1 to 5)
prints 1 2 3 4, no message
logic
for i in range(3) (no colon)
SyntaxError (Python points at the line)
syntax
Four more broken programs. Each row shows what happens and the kind of error. A missing int() gives a run-time error. A misspelt name gives a run-time error. A wrong stop value gives no message at all. A missing colon is a syntax error, and Python points at the line.
Class activity
Bug cards (no computer). On six cards write one short loop program each. Make two with a syntax error (a missing colon, a capital F), two with a run-time error (a name that was never created, range of a text value) and two with a logic error (a stop value that is one too small, a start value of 0 where 1 is needed). Shuffle them. A partner sorts the cards into three piles, syntax, run-time and logic, and explains what Python would do with each. Then fix each card with a coloured pen.
Practice
1
The user types 4 and then the heights 152, 148, 160 and 155. Draw a trace table with the columns n, height, top and give the output.
top starts at 0. 152 > 0 so top = 152. 148 > 152 is false. 160 > 152 so top = 160. 155 > 160 is false. The output is Tallest: 160.
2
The user types 3 laps with the times 61, 59 and 60, but the program prints Fastest: 0. Why? Correct it.
Python
1laps = int(input("How many laps? "))
2best = 03for lap inrange(laps):
4 time = int(input("Time in seconds? "))
5if time < best:
6 best = time
7print("Fastest:", best)
Output
Fastest: 0
best starts at 0, so no time is ever smaller than it and it is never replaced. Start best at a large number such as 999 (or use the first time typed).
3
Name the kind of error in each line: (a) for i in range(5) with no colon, (b) print(total) when total was never created, (c) range(1, 5) when the aim was the numbers 1 to 5.
(a) syntax error, because Python cannot read the line. (b) run-time error: NameError, because the program stops at that line. (c) logic error, because it runs but misses 5; the stop value should be 6.
4
Write a Python program that asks how many numbers there will be, then asks for each one, and prints the smallest.
Model answer (lines are separated by /, and [indent] means four spaces at the start of the line): count = int(input("How many? ")) / smallest = 9999 / for n in range(count): / [indent] value = int(input("Number? ")) / [indent] if value < smallest: / [indent] [indent] smallest = value / print("Smallest:", smallest)