Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Preview

Finding and fixing bugs

This is the first page of the lesson. Read it and have a go at the activities. The rest is in the full lesson.

45 minutes

Be a detective

Imagine a shop app that charges 1000 pounds for a sandwich. The customer is angry. The shop owner is worried. Somebody must find the cause.

Even good programmers write bugs. What makes them good is how fast they find and fix them. They work like a detective: look at the clues, make a guess, test it.

Key words

WordWhat it meansExample
DebugTo find and fix mistakes in code.
BugA mistake in a program.
LocateTo find where in the code the mistake is.
TracebackThe message Python shows when an error stops the program. It names the line.
Print statementA print line added to see the value of a variable.
DebuggerA tool that runs your code step by step.
BreakpointA marker where the debugger pauses the program.
VariableA name that holds a value.
Trace tableA table where you write the value of each variable after each step.
RobustKeeps working, or stops safely, when something unexpected happens.
Syntax errorThe code breaks the rules of Python. The program does not start.a missing colon
Runtime errorThe program starts, then crashes while it runs.dividing by zero
Logic errorThe program runs but gives a wrong answer. There is no message.a total that is 1 too big

The cells students fill in are left blank in this preview.

Run it: read the message

Code: python
total = 10
bonus = 5
print("Pay is", totl + bonus)

# Run it. Python stops and shows a message.
# Which line number does it give? What is the name of the error?

Read the message from the bottom

The message looks scary, but it is a gift. It tells you three things: where, what and why.

Start at the last line. It names the kind of error and says what is wrong. Then look up for the line number.

Parts of a Python error message

PartExampleWhat it tells you
Line numberline 3Where to look first
The code lineprint("Pay is", totl + bonus)The line that failed
Kind of errorNameErrorWhat type of problem it is
Explanationname 'totl' is not definedWhy it failed: a spelling mistake

The cells students fill in are left blank in this preview.

Debugging has two jobs

Locate the error: find where it is. Correct the error: fix it.

Do the jobs in order. Never change code at random. Make one change, then run the program again. If you change five things at once you will not know which one helped.

Put the debugging steps in order

Have a go

Put the debugging steps in order

3 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Your answers are marked here and are not kept. A college account keeps every student’s marks.

Three kinds of error

KindWhat happensExample
Syntax errorThe code breaks the rules of Python. The program does not start.A missing colon after if
Runtime errorThe program starts, then crashes while it runs.Dividing by zero
Logic errorThe program runs but gives a wrong answer. There is no message.Adding 1 too many to a count

The cells students fill in are left blank in this preview.

Quick check: which kind of error?

Have a go

Quick check: which kind of error?

3 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Your answers are marked here and are not kept. A college account keeps every student’s marks.

The lesson goes on

10 more parts of this lesson are in the full curriculum, with the tasks, the final check and the labs. A college account gives your students all of it.