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. It shows the values changing 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.
Step 1: read the code against the brief
| 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? | 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
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
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 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
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 questionsYour answers are marked here and are not kept. A college account keeps every student’s marks.