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
| Word | What it means | Example |
|---|---|---|
| Interpret | Read code and work out what it does. | say what 7 // 2 gives |
| Debug | Find and fix mistakes. | change = to == |
| Trace table | A table where you write each value after every step. | step, value |
| Operator | A symbol that does something to values. | + |
| Relational operator | An operator that compares two values. | >= |
| Boolean operator | And, or, not. They join True and False values. | a and b |
| Logic error | The 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 questionsWorked example: trace an expression
| Step | What Python does | Value so far |
|---|---|---|
| Code | `result = 7 // 2 + 7 % 2 * 3` | |
| 1 | 7 // 2 is the whole part of 7 divided by 2 | 3 |
| 2 | 7 % 2 is the remainder | 1 |
| 3 | 1 * 3 (multiply before add) | 3 |
| 4 | 3 + 3 | 6 |
| Result | result is 6 | 6 |
The cells students fill in are left blank in this preview.
Try it: predict, then run
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 questionsFive operator bugs
| Bug | Wrong code | Right 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
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.
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
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
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 questionsRemember
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.