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

Preview

Find and fix defects in code

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

The till charges too much

A member pays 6 pounds for an hour. The sign says members pay 4.

Is the sign wrong? Is the till wrong? Is the code wrong? You do not know yet.

A defect is a mistake in a program. To find one, follow a method.

Key words

WordWhat it meansExample
DefectA mistake in a program. Also called a bug.price is wrong
DebugTo find and fix a defect.fix the price
Syntax errorCode that breaks the rules of the language. It will not run.a missing colon
Runtime errorCode that starts, then stops with an error message.a name that does not exist
Logic errorCode that runs but gives the wrong answer.the wrong price
Trace tableA table you fill in by hand. It shows the values changing line by line.total: 0, 1, 1
TestA check that shows if code gives the right answer.price_for(True, 2) is 8

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

Step 1: read the code against the brief

Question to askExample for price_for
What should this code do?Members pay 4 an hour. Visitors pay 6 an hour.
What does it really do?Run it with a member for 2 hours. It shows 12, not 8.
Where do they differ?The two rates are in the wrong places. They are swapped.

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

Try it: a syntax error

Code: python
def is_child(age)
    return age < 16

print(is_child(12))

# Press Run. Read the message.
# Python says where it got stuck.
# Fix the line and run it again.

Try it: a runtime error

Code: python
def total_price(hours):
    rate = 6
    return rate * hour

print(total_price(2))

# This starts, then crashes.
# Which name does Python not know?
# Fix it and run it again.

Trace a logic error by hand

Line runone["member"]Is it Sam?total
total = 00
first bookingSamyestotal = 1
second bookingSamyestotal = 1 again, not 2
third bookingAishano1
The answer1, but Sam has 2 bookings

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

Change one thing at a time

data-callouts.warning.label

If you change three things and the code works, you do not know which change fixed it. If you change three things and it breaks, you do not know which one broke it. Change one thing. Run. Then decide.

Quick check: kinds of error

Have a go

Quick check: kinds of error

4 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

6 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.