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
| Word | What it means | Example |
|---|---|---|
| Defect | A mistake in a program. Also called a bug. | price is wrong |
| Debug | To find and fix a defect. | fix the price |
| Syntax error | Code that breaks the rules of the language. It will not run. | a missing colon |
| Runtime error | Code that starts, then stops with an error message. | a name that does not exist |
| Logic error | Code that runs but gives the wrong answer. | the wrong price |
| Trace table | A table you fill in by hand to follow values line by line. | total: 0, 1, 1 |
| Test | A 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 ask | Example 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
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
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 run | one["member"] | Is it Sam? | total |
|---|---|---|---|
| total = 0 | 0 | ||
| first booking | Sam | yes | total = 1 |
| second booking | Sam | yes | total = 1 again, not 2 |
| third booking | Aisha | no | 1 |
| The answer | 1, 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 questionsStage 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.
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