Be the computer
A phone app says the average of your two test scores, 4 and 6, is 7. That cannot be right. It should be 5.
How do you find the mistake? You do what the computer does. You follow each step, one at a time, and write down every value.
Key words
| Word | What it means | Example |
|---|---|---|
| Trace table | A table where you write the value of each variable after every step. | n is 1, then 2, then 3 |
| Algorithm | A list of clear steps in order that solves a problem. | a recipe |
| Loop | Steps that repeat. | add 1 until n is 5 |
| Variable | A name that holds a value. | total |
| Output | What the program shows or gives back. | the total |
| Syntax error | A mistake in the spelling or rules of the language. The program will not start. | a missing colon |
| Logic error | The program runs but gives a wrong answer. | using > when you need >= |
| Runtime error | The program starts but crashes while it runs. | divide by zero |
| Debug | Find and fix mistakes. | change > to >= |
The cells students fill in are left blank in this preview.
Try it: what does it do?
a = 8
b = 5
if a > b:
biggest = a
else:
biggest = b
print(biggest)
# What is the purpose of this algorithm?
# Change a and b and run it again.
Make a free account to save your work and track your progress.
Find the purpose
To find what an algorithm is for, ask three questions. What goes in? What comes out? What happens in the middle?
The code above takes two numbers. It gives back the bigger one. Its purpose is to find the biggest of two numbers.
Match the code to its purpose
Have a go
Loading…
Your answers are marked here and are not kept. A college account keeps every student’s marks.
An algorithm to trace
total = 0
n = 1
while n <= 4:
total = total + n
n = n + 1
print(total)
Make a free account to save your work and track your progress.
Worked example: a trace table
| Step | n | total | Is n 4 or less? | Output |
|---|---|---|---|---|
| Start | 1 | 0 | yes | |
| After loop 1 | 2 | 1 | yes | |
| After loop 2 | 3 | 3 | yes | |
| After loop 3 | 4 | 6 | yes | |
| After loop 4 | 5 | 10 | no, stop | |
| 5 | 10 | 10 |
The cells students fill in are left blank in this preview.
How to read the table
Each row is one moment while the program runs. In loop 1, total goes from 0 to 0 + 1 = 1. In loop 2, it goes to 1 + 2 = 3. In loop 3, 3 + 3 = 6. In loop 4, 6 + 4 = 10.
When n becomes 5, the question "is n 4 or less?" is no. The loop stops and the program shows 10.
Finish the trace
Have a go
Finish the trace
3 questionsTrace a choice
| Mark | Is mark 40 or more? | Output |
|---|---|---|
| 35 | no | Fail |
| 40 | yes | Pass |
| 72 | yes | Pass |
The cells students fill in are left blank in this preview.
Write a row for every change
Tip
Draw one column for each variable. Write a new row each time a value changes. Do not skip a row, even when it is slow. Then run the code to check.
Three kinds of error
| Kind | What happens | Example |
|---|---|---|
| Syntax error | The program will not start. Python tells you the line. | if age < 16 with no colon at the end |
| Runtime error | The program starts and then crashes. | Dividing a number by 0 |
| Logic error | The program runs and gives the wrong answer. No message. | Using > 18 when an 18-year-old is an adult |
The cells students fill in are left blank in this preview.
Which kind of error?
Have a go
Which kind of error?
5 questionsTry it: find the bug
a = 4
b = 6
average = a + b / 2
print(average)
# The average of 4 and 6 should be 5.
# It prints 7.0. Why?
# Trace it: which does Python do first, the + or the /?
# Fix the line and run it again.
Make a free account to save your work and track your progress.
Your turn: fix three logic errors
Three functions each have one logic error. Find it and fix it.
- average should add two numbers and divide by 2.
- is_adult should say if somebody is 18 or over.
- total_to should add the whole numbers from 1 up to n.
Press Run Tests. If you are stuck, write a trace table on paper.
def average(a, b):
# The average of two numbers: add them, then divide by 2.
return a + b / 2
def is_adult(age):
# An adult is 18 or over.
return age > 18
def total_to(n):
# Add the whole numbers from 1 up to n. total_to(4) is 1+2+3+4 = 10.
total = 0
for number in range(1, n):
total = total + number
return total
Make a free account to save your work, or press Start lab to work through the whole lesson.
Watch: trace it, find the error, fix it
Press Play on the short film below. It traces the loop from this lesson with a trace table, one row at a time. Then it names the three kinds of error, traces total_to to find why its answer is wrong, and fixes it.
Film: trace it, find the error, fix it
Trace it, find the error, fix it
Every scene, as text
- A trace table has a column for each variable and a new row each time a value changes. Be the computer: at the start, total is 0 and n is 1. Is n 4 or less? Yes, so the loop runs.
- Each row is one moment while the program runs. In loop 1, total goes from 0 to 0 + 1 = 1 and n becomes 2; in loop 2, 1 + 2 = 3; in loop 3, 3 + 3 = 6; in loop 4, 6 + 4 = 10, and n becomes 5. Is 5 4 or less? No, so the loop stops, and print(total) shows 10.
- When an answer is wrong, name the kind of error. A syntax error is a mistake in the spelling or rules of the language, such as a missing colon: the program will not start. A runtime error, such as dividing by zero: the program starts, then crashes. A logic error, such as using > when you need >=: the program runs, but gives a wrong answer, and no message.
- Myth (false): a logic error always stops the program with a message. total_to(4) should add 1 + 2 + 3 + 4, which is 10, but this total_to prints 6, and Python shows no message at all.
- Trace it to find the error. range(1, n) stops before n, so with n as 4, number is 1, 2 and 3, and total goes 1, 3, 6. 4 is never added: the loop runs one time too few, so it prints 6.
- The fix: range(1, n + 1) stops before n + 1, so with n as 4, number goes up to 4, total reaches 10 and the program prints 10. Then run the tests again.
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
Exit quiz
6 questionsRemember
Important
Ask what goes in and what comes out. Use a trace table to find the output. Name the kind of error. Then fix it and test again.