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

Preview

Read, write and fix choices and loops

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

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.

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.

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.

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.

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)

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

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

The lesson goes on

5 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.