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

Free lesson

ESP task 2 (1 of 2): Find and fix defects in code

The whole lesson, free. Read it and have a go at the activities. To work the tasks and labs in the real lesson, press Start lab, with no account needed.

45 minutes

A quick check that you are not a bot, then your own lab opens on the interactive lesson.

Already have an account? Sign in

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 to follow values 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.

Read the code against the brief first

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?Once the name hour is fixed, a member for 2 hours 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.

Make a free account to save your work and track your progress.

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.

Make a free account to save your work and track your progress.

Match the message to its meaning

Have a go

Loading…

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

The debug loop

flowchart TD
  a([Run the test]) --> b{Does it pass?}
  b -->|yes| z([Done: write it in the log])
  b -->|no| c[Read the message and the line]
  c --> d[Say what should happen]
  d --> e[Trace the code by hand]
  e --> f[Change ONE thing]
  f --> a

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

Warning

Many people change three things at once to save time. Then they cannot tell which change fixed the code, or which one broke it. Change one thing. Run the test. Then decide.

Check: kinds of error

Have a go

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

Stage 2: fix the booking code

This is stage 2 of the Hillside practice project. Somebody else wrote this code. It has defects: at least one in each function. Run the tests. Read each message. Trace the code by hand. Change one thing at a time. Press Run Tests until every test passes. Write down each defect you find.

Code: python
def price_for(is_member, hours):
    # Members pay 4 pounds an hour. Visitors pay 6 pounds an hour.
    rate = 6 if is_member else 4
    return rate * hour


def is_free(bookings, court, hour):
    # A court is free if nobody has the same court at the same hour.
    for one in bookings:
        if one["court"] == court or one["hour"] == hour:
            return False
    return True


def count_for(bookings, member):
    # How many bookings does this member have?
    total = 0
    for one in bookings:
        if one["member"] == member:
            total = 1
    return total

Make a free account to save your work, or press Start lab to work through the whole lesson.

Fix the cause, not the test

Important

Never change a test to make it pass. The test comes from the brief. Fix the code. Keep the names and the layout of the code you were given. Add or remove only what you must, and write down why.

A defect log is evidence of your thinking

Write each defect in a table as you go: where it was, what was wrong, what you changed and the test result.

The log is evidence. It shows a marker how you think. It also helps you if the same defect comes back.

Stage 2: write your defect log

Make a defect log for the booking code. Use a table with the columns: Number, Function, What was wrong, Kind of error, What I changed, Why I changed it, Test result. Write one row for every defect you fixed. Then write two sentences: what helped you most to find them, and what you will do next time.

A teacher marks this in a college. Nothing you write here is kept: it goes when you leave the page.

Make a free account to save your work, or press Start lab to work through the whole lesson.

Key words crossword

Have a go

Loading…

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

Final check

Have a go

Final check

5 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

Teach this lesson with your class

On a college account your students work the tasks and the labs as well, and every mark is kept for you.