Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Preview

Trace it, find the error, fix it

This is the first page of the lesson. Read it and have a go at the activities. The rest is in the full lesson.

45 minutes

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

WordWhat it meansExample
Trace tableA table where you write the value of each variable after every step.n is 1, then 2, then 3
AlgorithmA list of clear steps in order that solves a problem.a recipe
LoopSteps that repeat.add 1 until n is 5
VariableA name that holds a value.total
OutputWhat the program shows or gives back.the total
Syntax errorA mistake in the spelling or rules of the language. The program will not start.a missing colon
Logic errorThe program runs but gives a wrong answer.using > when you need >=
Runtime errorThe program starts but crashes while it runs.divide by zero
DebugFind and fix mistakes.change > to >=

The cells students fill in are left blank in this preview.

Try it: what does it do?

Code: python
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.

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.

An algorithm to trace

Code: python
total = 0
n = 1

while n <= 4:
    total = total + n
    n = n + 1

print(total)

Worked example: a trace table

StepntotalIs n 4 or less?Output
Start10yes
After loop 121yes
After loop 233yes
After loop 346yes
After loop 4510no, stop
Print51010

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 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Your answers are marked here and are not kept. A college account keeps every student’s marks.

The lesson goes on

9 more parts of this lesson are in the full curriculum, with the tasks, the final check and the labs. A college account gives your students all of it.