The 16-year-old who paid the child fare
Imagine a bus app charges a 16-year-old the child fare. The company loses money every day. Nobody sees it for a month.
The mistake is one small symbol in one line of code. Finding a mistake like this is a skill. You will practise it now.
Key words
| Word | What it means | Example |
|---|---|---|
| Bug | A mistake in a program. | |
| Debug | To find a bug and fix it. | |
| Syntax error | The code breaks the rules of Python. The program does not start. | a missing colon |
| Runtime error | The program starts but crashes while it runs. | dividing by zero |
| Logic error | The program runs but gives a wrong answer. | <= instead of < |
| Trace table | A table where you write the value of each variable after each step. | |
| Variable | A name that holds a value. | |
| Loop | A part of a program that repeats. | |
| Iteration | One run through a loop. |
The cells students fill in are left blank in this preview.
Three kinds of error
| Kind | What happens | How you find it |
|---|---|---|
| Syntax error | Python stops at once and shows a message | Read the line number in the message |
| Runtime error | The program crashes part of the way | Read the message. Ask what data caused it |
| Logic error | No message, but the answer is wrong | Trace the code with a table. Test small parts |
The cells students fill in are left blank in this preview.
Fix it: a syntax error
age = 15
if age < 16
print("Child fare")
else:
print("Adult fare")
# Run it and read the message. Which line does it point to?
# Fix the line and run it again.
Logic errors are the hard ones
A syntax error shows a message. A logic error shows no message. The program runs and gives a wrong answer.
The best tool is a trace table. Go through the code one line at a time. Write down each value. Compare what you wrote with what you expected.
Fix it: the wrong edge
age = 16
if age <= 16:
print("Child fare")
else:
print("Adult fare")
# A 16-year-old should pay the adult fare. Fix the condition.
# Then test the ages 15, 16 and 17.
Fix it: wrong by one
# This should print the numbers 1 to 5.
for i in range(5):
print(i)
# It prints 0 to 4. Change the range so it prints 1 to 5.
Fix it: the wrong order
total = 0
print("Total:", total)
total = total + 5
total = total + 3
# It prints 0. It should print 8.
# Move one line so the steps run in the right order.
Trace table: a loop that never stops
| Step | count | count < 3 ? | What happens |
|---|---|---|---|
| Start | 0 | True | The code: count = 0, then while count < 3: print(count) |
| After iteration 1 | 0 | True | The line that adds 1 is missing |
| After iteration 2 | 0 | True | The loop runs again |
| After iteration 3 | 0 | True | It will never become False |
The cells students fill in are left blank in this preview.
Read this code
# i != 2 means: i is not equal to 2.
total = 0
for i in range(1, 4):
if i != 2:
total = total + i
print(total)
Quick check: what does it print?
Have a go
Quick check: what does it print?
3 questionsYour answers are marked here and are not kept. A college account keeps every student’s marks.