Debugging Python programs

Debugging is a required topic in every UK GCSE Computer Science specification, and reading Python's error messages accurately is a marked skill. This unit covers the three categories of bug (syntax, runtime, logic), how to read a Python traceback from bottom to top, print-based debugging, and a systematic bug-hunting approach: reproduce, isolate, hypothesise, test. Every example runs in the browser so learners can trigger the errors themselves and see the tracebacks live. Perfect for classroom teaching, GCSE and A Level revision, and independent or home-schooled learners.


What you'll learn

  • Recognise the three types of error: syntax, runtime, logic
  • Read a Python traceback: the last line is usually the useful one
  • Common errors: NameError, TypeError, ValueError, IndexError, ZeroDivisionError
  • Use print() to inspect variable values at key points
  • Reproduce, isolate, hypothesise, test — a systematic approach

UK GCSE topic coverage

  • Print statements
  • Variables
  • Data types and casting
  • Arithmetic operators
  • If statements
  • Logical operators
  • Nested selection
  • For loops
  • While loops
  • Arrays
  • 2D arrays
  • String handling
  • Functions
  • Procedures
  • Random and libraries

Worked example

A classic logic error: off-by-one when looping

# This is meant to print 1..10 but only prints 1..9.
for i in range(1, 10):
    print(i)
# Fix: use range(1, 11).

Try a full unit free with a RunPy account

Sign up free and get the whole of Unit 1 (Input, Output and Variables) with checked tasks and instant feedback, plus a selection of the exam-style questions so you can practice on real GCSE-style content before upgrading.


Lessons in this unit

Full interactive lessons with checked tasks are available to signed-in RunPy users. Unit 1 and a selection of exam-style questions are free with a RunPy account.

  1. Print statements
  2. Variables
  3. Data types and casting
  4. Arithmetic operators
  5. If statements
  6. Logical operators
  7. Nested selection
  8. For loops
  9. While loops
  10. Arrays
  11. 2D arrays
  12. String handling
  13. Functions
  14. Procedures
  15. Random and libraries

Frequently asked questions

What is the difference between a syntax error and a runtime error?
A syntax error stops the code from running at all — Python can't parse it. A runtime error happens while the code is running, e.g. dividing by zero or indexing past the end of a list. A logic error runs without complaint but produces the wrong answer.
How do I read a Python traceback?
Read from the bottom: the last line names the exception (e.g. NameError) and gives a hint. The lines above trace which function called which, ending at the line where the error happened.
Is print-based debugging OK?
Yes. It's a legitimate technique used by professional developers and it's the standard approach at GCSE. Just remember to remove the prints once the bug is fixed.