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.4 (3 of 3): Read, write and fix code with operators

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

Read it before you run it

A friend writes 10 - 4 / 2 and says the answer is 3. Is your friend right?

A good programmer can read code and say the answer before running it. This is the skill of interpreting code. It also helps you find bugs.

Key words

WordWhat it meansExample
InterpretRead code and work out what it does.say what 7 // 2 gives
DebugFind and fix mistakes.change = to ==
Trace tableA table where you write each value after every step.step, value
OperatorA symbol that does something to values.+
Relational operatorAn operator that compares two values.>=
Boolean operatorAnd, or, not. They join True and False values.a and b
Logic errorThe program runs and gives a wrong answer.> instead of >=

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

Is your friend right?

Have a go

Is your friend right?

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

Worked example: trace an expression

StepWhat Python doesValue so far
Code`result = 7 // 2 + 7 % 2 * 3`
17 // 2 is the whole part of 7 divided by 23
27 % 2 is the remainder1
31 * 3 (multiply before add)3
43 + 36
Resultresult is 66

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

Try it: predict, then run

Code: python
a = 7 // 2 + 7 % 2 * 3
b = (2 + 3) * 4 - 1
c = 5 > 3 and not 2 > 4
d = 17 % 5 == 2

print(a)
print(b)
print(c)
print(d)

# Write your predictions on paper first.
# Then run it and check.

Make a free account to save your work and track your progress.

Interpret the code

Have a go

Interpret the code

4 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

Five operator bugs

BugWrong codeRight code
One = where you need ==`if age = 16:``if age == 16:`
Missing brackets`a + b / 2``(a + b) / 2`
Boundary wrong`age > 18` for adult`age >= 18`
Half a comparison`age == 15 or 16``age == 15 or age == 16`
Wrong operator for the job`n // 2 == 0` for even`n % 2 == 0`

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

Match the bug to the fix

Have a go

Loading…

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

Try it: find the bug

Code: python
age = 15

if age == 15 or 16:
    print("Age is 15 or 16")
else:
    print("Other age")

# Change age to 40. It still says 15 or 16.
# Python reads 16 on its own as True. (Any number except 0 counts as True.)
# Fix: if age == 15 or age == 16:

Make a free account to save your work and track your progress.

Your turn: fix four bugs

Four functions each have an operator bug. Find each one and fix it. is_teenager is for ages 13 to 19, including 13 and 19. average_of_two adds two numbers and divides by 2. is_even is True for even numbers. age_ok is True for exactly 15 or exactly 16. Press Run Tests.

Code: python
def is_teenager(age):
    # A teenager is 13 to 19 years old, including 13 and 19.
    return age > 13 and age < 19


def average_of_two(a, b):
    # Add the two numbers, then divide by 2.
    return a + b / 2


def is_even(number):
    # Even numbers leave no remainder when divided by 2.
    return number // 2 == 0


def age_ok(age):
    # The age must be exactly 15 or exactly 16.
    return age == 15 or 16

Make a free account to save your work, or press Start lab to work through the whole lesson.

Watch: read it, then fix it

Press Play on the short film below. It traces one line a step at a time, then reads two programs that run with no error message, finds each bug and fixes it.

Film: reading and fixing code with operators

Reading and fixing code with operators

Every scene, as text

  1. Interpret: read code and work out what it does. A trace of result = 7 // 2 + 7 % 2 * 3, one step at a time: 7 // 2 is 3, the whole part of 7 divided by 2; 7 % 2 is 1, the remainder; 1 * 3 is 3, as //, % and * come before +, worked left to right; then 3 + 3 is 6, so result is 6.
  2. Myth (false): no error message means the code is right. The average of 4 and 6 is 5, but with a set to 4 and b set to 6, print(a + b / 2) runs with no error and shows 7.0. This is a logic error: the program runs and gives a wrong answer.
  3. Trace it to find the bug. / comes before +, so b / 2 goes first: 6 / 2 is 3.0. Then 4 + 3.0 is 7.0. Only b was divided by 2.
  4. The fix is brackets, which are worked out first: print((a + b) / 2) shows 5.0, the average. Traced, 4 + 6 is 10 first, then 10 / 2 is 5.0. / always gives a decimal number.
  5. A teenager is 13 to 19, including 13 and 19. Test the edge value: with age set to 13, print(age > 13 and age < 19) shows False, as 13 > 13 is False. > leaves 13 out, another logic error with no error message.
  6. Fix the boundary with >= and <=: print(age >= 13 and age <= 19) shows True, as 13 >= 13 is True. Test the other edge value, 19, as well.

Your turn: write the conditions

Write two functions with operators. ticket_price gives 0 under age 5, 5 for students and anyone under 16, and 10 for everyone else. is_leap_year follows the leap year rule in the comments. Use and, or and the modulus operator. Press Run Tests.

Code: python
def ticket_price(age, is_student):
    # Under 5: free (0). Students and anyone under 16: 5 pounds. Everyone else: 10 pounds.
    return None


def is_leap_year(year):
    # A leap year divides by 4 with no remainder. But a year that divides by 100 with no
    # remainder is not a leap year, unless it also divides by 400 with no remainder.
    return None

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

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

Work out the order. Check the boundary. Use == to compare. Write both sides of an or. Use brackets when unsure. Test the edge values.

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.