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

Free lesson

1.2 (3 of 3): Trace it, find the error, fix it

The whole lesson, free. Read it and have a go at the activities. To work the tasks and labs in the real lesson, press Start lab, with no account needed.

45 minutes

A quick check that you are not a bot, then your own lab opens on the interactive lesson.

Already have an account? Sign in

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.

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

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

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

Trace a choice

MarkIs mark 40 or more?Output
35noFail
40yesPass
72yesPass

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

KindWhat happensExample
Syntax errorThe program will not start. Python tells you the line.if age < 16 with no colon at the end
Runtime errorThe program starts and then crashes.Dividing a number by 0
Logic errorThe 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 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

Try it: find the bug

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

  1. average should add two numbers and divide by 2.
  2. is_adult should say if somebody is 18 or over.
  3. 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.

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

  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. 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.
  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 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

Remember

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.

Teach this lesson with your class

On a college account your students work the tasks and the labs as well, and every mark is kept for you.