GoalUse a test plan to find bugs, and name syntax, runtime and logic errors.
New words
test plan · a table of tests with test data and the expected outcome, decided before the program is runbug · a mistake in a program; debugging means finding and fixing bugssyntax error · a mistake in the rules of the language; the program will not startlogic error · the program runs, but gives the wrong answer
Explanation
Bugs are mistakes in programs, and debugging means finding and fixing them. A syntax error breaks the rules of Python, so the program will not start. A runtime error stops the program while it is running, such as dividing by zero. A logic error does not stop the program, but it gives a wrong answer. To find logic errors, use a test plan. Work out the expected outcome by hand before you run the program. Then run the tests, write the actual outcome and mark pass or fail. If the two do not match, there is a bug.
This is the program that the test plan tested. With 20 and 15 typed, it shows 35, as in test 1. The bug is on line 3, where + is used instead of −. The program runs, so this is a logic error. A syntax error would have stopped it at once.
Program
What happens
Kind of error
print("Hi"
SyntaxError: '(' was never closed
syntax error
print(10 / 0)
ZeroDivisionError: division by zero
runtime error
print(7 - 2 * 3)
shows 1, but the programmer wanted (7 - 2) * 3 = 15
logic error
Here are the three kinds of error side by side. Each program is short, so you can see the difference.
Incorrect line
Problem
Correct line
print("Hello", name))
one closing bracket too many
print("Hello", name)
print('Done")
the two quotation marks do not match
print("Done")
colour = "red" then print("Colour:", color)
the variable is called colour, not color
print("Colour:", colour)
price = input("Price? ") then print(price * 2)
price is a string and has no cast, so * 2 repeats the text
price = float(input("Price? "))
Many bugs are small typing mistakes. Check four things in every program: brackets, quotation marks, variable names and casting.
Class activity
Bug hunt (no computer): write a four-line Python program on paper that should work out how many days are in a number of weeks. Without telling your partner, put one bug in it. Write a test plan with two tests, working out the expected outcomes by hand first. Swap papers. Your partner runs the program in their head, fills in the actual outcomes and the pass/fail column, and then circles the bug. Which kind of error was it? If you have a computer or tablet, type the buggy program into IDLE with an adult and compare.
Practice
1
Work out the expected outcome for each test by hand. The program should change minutes into seconds.
Test
Test data
Expected outcome
1
minutes = 3
?
2
minutes = 10
?
3
minutes = 0
?
Test 1: 180. Test 2: 600. Test 3: 0. You must work these out before running, because otherwise a wrong actual outcome might look right.
2
A test plan expects 300 when 3 is typed. The program shows something else. What kind of error is this, and how do you fix it?
Python
1length = int(input("Length of ribbon in metres? "))
2cm = length * 103print("Centimetres:", cm)
Output
Centimetres: 30
The program shows 30, so it runs but gives a wrong answer. That is a logic error. The multiplier is wrong: 3 * 10 is 30. Line 2 should be cm = length * 100.
3
Find the mistake in each line.
Line
Mistake?
name = input(Enter your name")
(a) ?
print("Total:", totl) # total was created
(b) ?
age = input("Age? ") then print(age + 1)
(c) ?
(a) The opening quotation mark before Enter is missing. (b) The variable is called total, not totl, so Python reports a NameError. (c) age is a string and has no cast, so age + 1 gives a TypeError.
4
Is each error a syntax error, a runtime error or a logic error? (a) The program will not start because a bracket is missing. (b) The program runs but shows 8 when the answer should be 12. (c) The program stops with ZeroDivisionError.