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.
Make a free account to save your work and track your progress.
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.
Make a free account to save your work and track your progress.
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.
Make a free account to save your work and track your progress.
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.
Make a free account to save your work and track your progress.
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)
Make a free account to save your work and track your progress.
Quick check: what does it print?
Have a go
Quick check: what does it print?
3 questionsMatch the problem to the kind of error
Have a go
Loading…
Your answers are marked here and are not kept. A college account keeps every student’s marks.
Put the program in order
Have a go
Put the program in order
3 questionsYour turn: fix and write
Three functions have a bug each, and one is for you to write. Read the comments.
- Fix
is_teenager. - Fix
count_evens. The testnumber % 2 == 0asks if a number is even. The sign%gives the remainder after dividing: 6 % 2 is 0 and 7 % 2 is 1. - Fix
sum_to. - Write
countdownwith a while loop.
Use a trace table on paper if you get stuck. Press Run Tests to check.
def is_teenager(age):
# A teenager is 13 to 19 years old. Both 13 and 19 count.
# This code has a bug. Find it and fix it.
if age > 13 and age < 19:
return True
return False
def count_evens(numbers):
# Count how many numbers in the list are even.
# This code has a bug. Find it and fix it.
count = 0
for number in numbers:
count = 0
if number % 2 == 0:
count = count + 1
return count
def sum_to(n):
# Add the numbers from 1 up to and including n.
# This code has a bug. Find it and fix it.
total = 0
for i in range(n):
total = total + i
return total
def countdown(n):
# Write this one yourself with a while loop.
# Give back a list that counts down from n to 1.
# For n = 3 the list is [3, 2, 1].
return []
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.
Exit quiz
Have a go