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

Free lesson

2.6 (3 of 3): Read, write and fix choices and loops

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

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

WordWhat it meansExample
BugA mistake in a program.
DebugTo find a bug and fix it.
Syntax errorThe code breaks the rules of Python. The program does not start.a missing colon
Runtime errorThe program starts but crashes while it runs.dividing by zero
Logic errorThe program runs but gives a wrong answer.<= instead of <
Trace tableA table where you write the value of each variable after each step.
VariableA name that holds a value.
LoopA part of a program that repeats.
IterationOne run through a loop.

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

Three kinds of error

KindWhat happensHow you find it
Syntax errorPython stops at once and shows a messageRead the line number in the message
Runtime errorThe program crashes part of the wayRead the message. Ask what data caused it
Logic errorNo message, but the answer is wrongTrace the code with a table. Test small parts

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

Fix it: a syntax error

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

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

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

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

Stepcountcount < 3 ?What happens
Start0TrueThe code: count = 0, then while count < 3: print(count)
After iteration 10TrueThe line that adds 1 is missing
After iteration 20TrueThe loop runs again
After iteration 30TrueIt will never become False

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

Read this code

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

Match 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 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 turn: fix and write

Three functions have a bug each, and one is for you to write. Read the comments.

  1. Fix is_teenager.
  2. Fix count_evens. The test number % 2 == 0 asks if a number is even. The sign % gives the remainder after dividing: 6 % 2 is 0 and 7 % 2 is 1.
  3. Fix sum_to.
  4. Write countdown with a while loop.

Use a trace table on paper if you get stuck. Press Run Tests to check.

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

Exit quiz

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

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.