AdvertisementYour brand could be hereYour brand hereOn every Darslab page · students, parents and teachersinfo@darslab.comAsk for pricesPricesinfo@darslab.com
☰ Year plan · Computing

Python loops

Week 6 · 1.4 Loops in text-based programming · 3 lessons
3

Best lap and bad lines

Computing 9: Topic 1.4, pp. 80–88
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 = 999
3bestLap = 0
4for lap in range(laps):
5    time = int(input("Time in seconds? "))
6    if time < best:
7        best = time
8        bestLap = lap + 1
9print("Fastest: lap", bestLap, "in", best, "seconds")
Output
Fastest: lap 4 in 57 seconds
Worked examples
laptime typedtime < best?bestbestLap
(before)9990
062yes621
158yes582
265no582
357yes574
460no574
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 in range(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.
ProgramWhat happensKind
n = input() … for i in range(n):TypeError: 'str' object cannot be interpreted as an integerrun-time
for i in range(3): print(j)NameError: name 'j' is not definedrun-time
for i in range(1, 5): (meant 1 to 5)prints 1 2 3 4, no messagelogic
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.
Python
1count = int(input("How many? "))
2top = 0
3for n in range(count):
4    height = int(input("Height? "))
5    if height > top:
6        top = height
7print("Tallest:", top)
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 = 0
3for lap in range(laps):
4    time = int(input("Time in seconds? "))
5    if time < best:
6        best = time
7print("Fastest:", best)
Output
Fastest: 0
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.
4
Write a Python program that asks how many numbers there will be, then asks for each one, and prints the smallest.