Problem Solving with Python 🐍
Boolean operators, while loops, if/elif/else, and functions — building on last year's if/if-else into real decision-making and reusable code.
🎯 What you'll unlock this lesson
- ✅ Use comparison and boolean operators to write conditions
- ✅ Repeat code with while loops
- ✅ Branch between more than two outcomes with if / elif / else
- ✅ Write and call your own functions with parameters and return values
💡 Comparison and boolean operators 2.2
Last year's Python used if / if-else with simple conditions. This year, conditions get richer with comparison operators and boolean operators that combine them.
Comparison operators
| Operator | Meaning |
|---|---|
| == | equal to |
| != | not equal to |
| < | less than |
| <= | less than or equal to |
| > | greater than |
| >= | greater than or equal to |
Boolean operators
| Operator | Meaning |
|---|---|
| and | true only if both sides are true |
| or | true if at least one side is true |
| not | flips true ↔ false |
🎢 Worked example: ride safety check
A theme park ride requires a rider to be taller than 115cm, or older than 3 and riding with an adult. As a boolean check for "needs an adult":
needs_adult = height <= 115 and age <= 3
| height | age | height <= 115 | age <= 3 | needs_adult |
|---|---|---|---|---|
| 110 | 2 | True | True | True |
| 110 | 5 | True | False | False |
| 120 | 2 | False | True | False |
| 120 | 5 | False | False | False |
and only gives True when both sides are True — that's why row 2 (tall enough already) is False.
🃏 Key-term flashcards tap to flip
🔁 The while loop 2.3
A while loop repeats a block of code as long as its condition stays True. It's the tool for "keep asking until the answer is valid."
while condition: # repeated while condition is True statement(s)
🎮 Interactive Step-Through: validating input signature move
This program keeps asking for a number until it gets one from 1–10. Hit Step to watch the variables and console update, line by line.
Variables
Console output
Run the simulator above — it feeds exactly those three attempts in. Both 15 and -2 fail the 1 <= n <= 10 check, so the loop keeps going; 7 passes and the loop ends.
🔀 if / elif / else 2.4
elif lets you check more than two possibilities in order — Python checks each condition top to bottom and runs the first one that's True, then skips the rest.
if condition1: statement(s) elif condition2: statement(s) else: statement(s)
📐 Worked example 2.6: area of a shape
One function, three shapes, branching on a shape string:
import math if shape == "circle": area = math.pi * radius ** 2 elif shape == "triangle": area = 0.5 * base * height elif shape == "rectangle": area = width * height else: area = None # unrecognised shape
Notice each elif is only checked if every condition above it was False.
🗣️ Activity 2.1 — trace it yourself first
For shape = "triangle", base = 6, height = 4: which branch runs, and what's the area?
shape == "circle" → False, skip. shape == "triangle" → True → area = 0.5 * 6 * 4 = 12.0. The rectangle branch is never even checked.
🧰 Functions 2.5
A function packages up a block of code under a name, so it can be reused without retyping it. def defines one; calling it by name runs it.
def function_name(parameter1, parameter2): statement(s) return value
Positional arguments
Matched to parameters by order.
classify(110, 5)
Keyword arguments
Matched to parameters by name — order doesn't matter.
classify(age=5, height=110)
🎮 Interactive Step-Through: a function that combines it all worked example 2.7
A function using boolean operators and if/elif/else, called with real arguments. Step through to see the parameters fill in, the branch get chosen, and the return value come back.
Variables
Console output
🧠 Mini-quiz — no cap, prove it
📝 Exam-style questions try, then peek
5 > 3 is True, 2 == 2 is True. True and True → True.
n = 0
while n < 3:
print(n)
n = n + 1Prints 0, 1, 2 — 3 times. When n becomes 3, n < 3 is False and the loop stops.
height <= 115 and age <= 3 → True and True → True → the first branch runs → returns "Not allowed on this ride".