Courses / Computer Science XI (HSSC-I) – FBISE Complete SLO-Based Course

3.8 Debugging: Errors, Testing Functions, Dry Runs and Breakpoints

Unit 3 – Programming Fundamentals · 60 min read · 12 MCQs to check yourself

Stop using print(): learn the VS Code debugger

1. Types of errors

Error type When found Example Python message
Syntax error Before running – breaks the rules of the language print("Hi" (missing bracket), if x = 5: SyntaxError
Runtime error (exception) While running – an impossible operation 10 / 0, int("abc"), a[10] on a 3-item list ZeroDivisionError, ValueError, IndexError, NameError, TypeError
Logical error Program runs but gives the wrong answer – no message! average = a + b / 2 instead of (a + b) / 2 none
Indentation error A kind of syntax error in Python missing indent after if IndentationError

Reading a traceback: the last line names the error type and message; the line above shows the file and line number where it happened. Start debugging there.

2. Why code inside functions is easier to test

Code written at top level runs all at once and mixes input, processing and output, so you cannot check one part separately. A function takes inputs as parameters and returns a value – you can call it with test values and compare the result with what you expect.

def average(a, b):
    return a + b / 2          # bug: wrong precedence

print(average(4, 6))           # expected 5.0

Output:

7.0

The test exposes the logical error. Fixed version with automatic checks:

def average(a, b):
    return (a + b) / 2

assert average(4, 6) == 5.0
assert average(0, 0) == 0
assert average(-2, 2) == 0
print("All tests passed")

Output:

All tests passed

assert condition does nothing if the condition is True and stops with AssertionError if it is False.

3. Dry run (desk check) by hand

Read the code line by line and record variables in a trace table. Find the bug: this code should print the sum of 1 to 5.

total = 0
i = 1
while i < 5:
    total += i
    i += 1
print(total)

Output:

10
i i < 5 ? total
1 True 1
2 True 3
3 True 6
4 True 10
5 False 10
The loop stops before adding 5 (an off-by-one error). Fix: while i <= 5: → output 15.

4. Using a debugger (VS Code, Thonny, IDLE)

  1. Click in the margin next to a line number to set a breakpoint (red dot).
  2. Start debugging (F5 in VS Code, the bug icon in Thonny). The program pauses at the breakpoint.
  3. Inspect the Variables panel – current values of all variables.
  4. Step over (F10) runs the next line; step into (F11) enters a function call; continue (F5) runs to the next breakpoint.
  5. Use a watch expression (e.g. total + i) to follow a value.
  6. Fix the code and run again.

5. Debugging strategies

for value in ["25", "abc", "0"]:
    try:
        n = int(value)
        print(100 / n)
    except ValueError:
        print("Please enter a number")
    except ZeroDivisionError:
        print("Cannot divide by zero")

Output:

4.0
Please enter a number
Cannot divide by zero

Python debugging in VS Code: reading tracebacks and fixing errors

Short and long questions

1. Differentiate between syntax, runtime and logical errors with one example each.

Model answer

Syntax error: violates language rules; detected before running – print("Hello" (missing bracket). Runtime error: occurs during execution – x = 10 / 0 (ZeroDivisionError). Logical error: program runs but gives wrong output – avg = a + b / 2 instead of (a + b) / 2.

2. What is a breakpoint? How does it help in debugging?

Model answer

A breakpoint is a marker set on a line in an IDE debugger (VS Code, Thonny) where execution pauses. While paused the programmer can inspect the current values of variables, step through the code line by line (step over/into) and see exactly where values go wrong, which helps locate logical errors.

3. What is a dry run? Dry-run the code and find the bug: total = 0; for i in range(1, 5): total += i; print(total) (intended to sum 1 to 5).

Model answer

A dry run is manually tracing a program line by line with a table of variable values. i = 1 → total 1; i = 2 → 3; i = 3 → 6; i = 4 → 10; loop ends (range stops before 5). Output 10 instead of 15. Bug: off-by-one – use range(1, 6).

4. Why is code written inside functions easier to test than code written outside functions?

Model answer

A function has clear inputs (parameters) and an output (return value), so it can be called repeatedly with different test values and its result compared with the expected answer (e.g. with assert). Code outside functions mixes input, processing and output and runs only once in sequence, so parts cannot be tested separately.

5. Explain different types of errors in Python and the methods used to debug a program, including testing functions, dry runs and breakpoints.

Model answer

Types of errors: (1) Syntax errors – violations of grammar such as missing colons or brackets, detected before running (SyntaxError, IndentationError). (2) Runtime errors – occur while running: ZeroDivisionError (10/0), ValueError (int("abc")), IndexError, NameError, TypeError. (3) Logical errors – the program runs but gives wrong results, e.g. wrong formula or off-by-one loop. Debugging methods:

  1. Read the traceback – the error type and line number.
  2. Testing functions – put code in functions, call them with known inputs and check return values (assert average(4, 6) == 5.0).
  3. Dry run – trace the code by hand with a trace table to find where values go wrong.
  4. Print statements – display intermediate values.
  5. Debugger and breakpoints – pause at a line, inspect variables, step over/into lines, use watches.
  6. Exception handling – try/except to handle expected runtime errors gracefully. Fix one thing at a time and re-test with normal, boundary and erroneous data.

This lesson ends with a quiz of 12 MCQs. Open the lesson on PrepOasis to take it and save your progress.

Previous: 3.7 Functions and Problem Decomposition · Next: Unit 3 Review, SLO Checklist and Unit Test

All lessons in Computer Science XI (HSSC-I) – FBISE Complete SLO-Based Course