GoalTrace programs with several inputs, and recognise the common mistakes that stop a program or make it give wrong answers.
New words
trace · follow a program by hand, step by step, with chosen inputsruntime error · a mistake that stops a program while it is runningtest data · values chosen to try out a program
Explanation
To trace a program that uses and and or, make a table with one row for each set of inputs. Write the result of each small condition and then the output. Choose inputs that make every branch run at least once. Mistakes fall into three families. A syntax error breaks the rules, so the program does not start, for example a missing colon or a capital AND. A runtime error stops the program while it runs, for example comparing text with a number using > or using a variable that has no value. A logic error lets the program run, but the answer is wrong, for example conditions in the wrong order or or where and was needed. Read the last line of the message, because it tells you the type and where to look.
Mistake
Example line
Family
capital AND
if a > 1 AND b < 9:
syntax error
text compared with a number
if age > 12: (age is still text)
runtime error
else if
else if x > 3:
syntax error
wrong order of conditions
if n >= 10 before n >= 20
logic error
Worked examples
slip
money
slip == "yes"
money >= 10
Printed
yes
10
True
True
Can go
yes
5
True
False
Almost ready
no
10
False
True
Almost ready
no
5
False
False
Not ready
The school trip program prints Can go when both conditions are true, Almost ready when only one is true and Not ready when neither is true. Here it is traced with four sets of inputs.
Line
What goes wrong
Family
if x > 5 and < 9:
right side is not a complete condition
syntax error
if x = 5:
= stores a value, == compares
syntax error
if x > 5 or x < 9: (meaning between)
true for every number
logic error
print(total) before total has a value
NameError
runtime error
A table of common errors with the type of each. Notice that a logic error does not show any message at all.
Python
1n = int(input("Number? "))
2if n >= 1or n <= 9:
3print("Accepted")
4else:
5print("Rejected")
Output
Accepted
A short program with an or in a range test: the intention was to print Accepted for numbers from 1 to 9. It accepts every number.
Class activity
Bug hunt (no computer). Write six short Python lines on separate slips of paper. Make three of them correct and put one mistake in each of the others: a capital AND, a missing colon, and an or where an and is needed. Give the slips to a partner, who sorts them into three piles: correct, program will not start, and program runs but is wrong. Check the piles together and explain each mistake in one sentence. Swap roles. If you have a computer or tablet, try the slips with an adult and compare the real messages with your piles.
Practice
1
Trace the program for the four pairs of inputs. What is printed each time?
Python
1pass_type = input("Pass? ")
2age = int(input("Age? "))
3if pass_type == "day"and age < 12:
4print("Child day pass")
5elif pass_type == "day"or age < 12:
6print("Check the price")
7else:
8print("Full price")
(day, 8): Child day pass. (day, 30): Check the price. (week, 9): Check the price. (week, 40): Full price. The branch order is and first, then or, then else.
2
Name the family of mistake (syntax, runtime or logic) for each line: (a) if x > 5 AND x < 9: (b) if x > 5 or x < 9: when you want numbers between 5 and 9 (c) if x == 5 with no colon (d) if age > 12: when age came from input without int().
(a) syntax error (b) logic error (c) syntax error (d) runtime error: TypeError, because text is compared with a number.
3
Write a Python program: ask for the day. If it is Saturday or Sunday print Weekend, otherwise print School day.
Model answer (lines are separated by /, and [indent] means four spaces at the start of the line): day = input("Day? ") / if day == "Saturday" or day == "Sunday": / [indent] print("Weekend") / else: / [indent] print("School day")
4
Rewrite in Python: IF NOT (lives = 0) AND level >= 3 THEN OUTPUT "Bonus" ENDIF
Model answer (lines are separated by /, and [indent] means four spaces at the start of the line): if not (lives == 0) and level >= 3: / [indent] print("Bonus"). Note: The single = becomes ==, the capitals become lower case, THEN becomes a colon and ENDIF disappears.