Python Foundations · Lesson 12 of 12

Testing and debugging

Normal, boundary and invalid data; trace and fix. Learn through short examples, practical tasks and targeted questions.

← All lessons

Testing and fixing programs

Testing checks whether a program behaves as intended for normal, boundary and invalid inputs. A trace table records variable values as the algorithm runs, making errors easier to isolate. Change one part at a time and rerun the same test.

Watch out: A program working for one example does not prove it is correct. Include boundary values and unexpected input in the test plan.

1 · Understand

Normal, boundary and invalid data; trace and fix. Study the code and predict what it does before running it. Change a value or string and observe how the output changes.

def discount(price):
    if price >= 20:
        return price * 0.9
    return price

for value in (19.99, 20, 20.01):
    print(value, discount(value))
Download example .py ↓

2 · Practise

Create a function that accepts marks from 0 to 100, then test -1, 0, 50, 100 and 101. Explain which are invalid, normal or boundary cases.

Plan your inputs, processing and outputs. Write the code in Mu, IDLE or PyCharm; test at least three cases.

Show a reference solution after attempting the task
def valid_mark(mark):
    return 0 <= mark <= 100

for mark in (-1, 0, 50, 100, 101):
    print(mark, valid_mark(mark))
Download reference solution .py ↓

3 · Debug the code

Identify the problem and explain how to fix it before revealing the answer.

def average(a, b):
    return a + b / 2
Reveal the corrected program and explanation
def average(a, b):
    return (a + b) / 2

Compare the two snippets. Identify the changed line, run the corrected version and explain the effect.

4 · Check your understanding

Which values are useful for checking an inclusive 0–100 input range?

5 · Challenge and reflect

Extension: Improve your solution to deal with unexpected input, add a second feature or display results more clearly.

Reflect: What was the hardest part? What would you test if someone else used your program?

Want extra practice? Try interactive Python tracing and debugging questions →