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

Planning and testing code

Week 3 · Unit 7.1: Block it out · 3 lessons
3

Tests and bugs

Computing 7: Unit 7.1, pp. 8–43
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.

TestTest dataExpected outcomeActual outcomePass/fail
1paid = 20, cost = 15Change: 5Change: 35fail
2paid = 50, cost = 32Change: 18Change: 82fail
Worked examples
Python
1paid = int(input("Money paid? "))
2cost = int(input("Cost? "))
3change = paid + cost
4print("Change:", change)
Output
Change: 35
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.
ProgramWhat happensKind of error
print("Hi"SyntaxError: '(' was never closedsyntax error
print(10 / 0)ZeroDivisionError: division by zeroruntime error
print(7 - 2 * 3)shows 1, but the programmer wanted (7 - 2) * 3 = 15logic error
Here are the three kinds of error side by side. Each program is short, so you can see the difference.
Incorrect lineProblemCorrect line
print("Hello", name))one closing bracket too manyprint("Hello", name)
print('Done")the two quotation marks do not matchprint("Done")
colour = "red" then print("Colour:", color)the variable is called colour, not colorprint("Colour:", colour)
price = input("Price? ") then print(price * 2)price is a string and has no cast, so * 2 repeats the textprice = 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.
TestTest dataExpected outcome
1minutes = 3?
2minutes = 10?
3minutes = 0?
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 * 10
3print("Centimetres:", cm)
Output
Centimetres: 30
3
Find the mistake in each line.
LineMistake?
name = input(Enter your name")(a) ?
print("Total:", totl) # total was created(b) ?
age = input("Age? ") then print(age + 1)(c) ?
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.