All subjects · Computer Science · WCP02 Practical Programming and Problem-solving

Testing, debugging, reviewing and refining

Edexcel IAL Computer Science · WCP02 Practical Programming and Problem-solving

Ensuring code fits its purpose means testing it methodically, finding and fixing errors, and then improving it. The first question of the sample paper was a program full of errors to fix.

Practise this in the app: instant marking and a fix for every mistake. Free, no email needed to start.

Practise this lesson

Types of error (spec 9.3.1)

ErrorWhat happensExample
Syntax errorThe code breaks Python's rules and will not run at allmissing colon after if; unclosed bracket; wrong indentation
Runtime error (exception)The program starts but crashes on a lineTypeError, NameError, UnboundLocalError, IndexError, ZeroDivisionError
Logic errorThe program runs but gives the wrong result< instead of <=; wrong index such as top + 1; total reset inside a loop

Test data

Plan tests before running them, with the expected result for each:

Compare actual results with expected results; any difference is a bug to fix.

Debugging techniques

n = 5
f = 1
while n > 1:
    f = f * n
    n = n - 1
    print("n =", n, "f =", f)

Output:

n = 4 f = 5
n = 3 f = 20
n = 2 f = 60
n = 1 f = 120
nf
51
45
320
260
1120

Trace table for the loop above (first row: before the loop)

Reviewing and refining

Once it works, review the code against the task: does it meet every requirement, handle every case, and is the output formatted as asked? Then refine it: remove repetition into subprograms, replace literals with constants, improve names and comments, and reduce the number of passes. Re-test after every change. In the exam, do not add features that were not asked for.

Worked example

A program should accept ages 11 to 18. Plan normal, boundary and erroneous test data.

  1. Normal: 14, accept
  2. Boundary: 11 and 18, accept; 10 and 19, reject
  3. Erroneous: "twelve", rejected with a message, no crash

Answer: Normal 14; boundary 10, 11, 18, 19; erroneous "twelve"

Key idea: Syntax errors stop code running, runtime errors crash it, logic errors give wrong answers; test with normal, boundary and erroneous data; debug with messages, prints, breakpoints and trace tables; then review and refine.

Check yourself

  1. A program runs without crashing but calculates an average that is always slightly too high. What kind of error is this?
    • Syntax error
    • Runtime error
    • Logic error
    • Type conversion error
    Show answer

    Logic error. It runs and finishes, so it is not a syntax or runtime error; the result is wrong, so it is a logic error.

  2. Which line contains a syntax error?
    • if total > 10
    • total = total + 1
    • print("Total:", total)
    • while total < 10:
    Show answer

    if total > 10. An if statement must end with a colon.

  3. A program accepts quantities from 1 to 50. What type of test data is 50?
    • Normal
    • Erroneous
    • Boundary
    • Extreme invalid
    Show answer

    Boundary. 50 is the largest valid value, at the edge of the range.

  4. What is displayed when this code runs? ```python a = 1 b = 1 for i in range(4): a, b = b, a + b print(a, b) ```
    Show answer

    5 8. Trace (a, b): (1, 1), (1, 2), (2, 3), (3, 5), (5, 8): 5 8.

  5. This code should display 9 but it does not. Which line number contains the error? ``` 1 values = [9, 2, 6, 1] 2 largest = 0 3 for i in range(1, len(values)): 4 if values[i] > largest: 5 largest = values[i] 6 print(largest) ```
    Show answer

    3. The loop starts at index 1, so values[0] (9) is never checked. It should be for i in range(0, len(values)):, or simply for i in range(len(values)):.

  6. In an IDE, what does setting a breakpoint do?
    • Deletes the line from the program
    • Pauses the program at that line so you can inspect variables and step on
    • Stops the program from ever reaching that line
    • Converts the line into a comment
    Show answer

    Pauses the program at that line so you can inspect variables and step on. A breakpoint pauses execution so you can watch variables and step through the following lines.

Practise with instant feedback

Sources

Last updated 8 November 2026. Spotted a mistake? Tell us.