Example 1: Data types and casting (program writing)
Question: Write a program that asks the user for two whole numbers and prints their sum.
Solution:
Python 3 example:
first = int(input("First whole number: "))
second = int(input("Second whole number: "))
print(first + second)
Walkthrough: input() returns a string, so each value is wrapped in int() to cast it to an integer. Without the casts, "5" + "3" would produce "53" (concatenation) instead of 8.
Exam tip: Cast input when a numeric calculation is required and handle invalid conversion when robustness is asked for. Stating why you cast ("because input returns a string") can earn an explanation mark.
Example 2: Selection with nested conditions (program writing)
Question: A shop gives a 10% discount on orders over £50 and 20% on orders over £100. Write a program that reads an order total and prints the amount to pay.
Solution:
Python 3 example:
order = float(input("Order total: "))
if order > 100:
payable = order * 0.8
elif order > 50:
payable = order * 0.9
else:
payable = order
print(f"{payable:.2f}")
Test £50 → £50.00, £100 → £90.00, £150 → £120.00. “Over” is strict: £100 does not receive 20%. Assume a non-negative numeric order.
Walkthrough: The conditions are tested from the largest threshold down. If order > 100 is checked first, an order of £150 correctly receives 20%. If the > 50 test came first, every large order would wrongly get only 10% — an ordering logic error.
Exam tip: Order overlapping conditions from most specific (highest) to least specific to avoid a common logic trap.
Example 3: Iteration and an array total (program writing)
Question: Store the five numbers 4, 8, 15, 16, 23 in an array, then output their total and average.
Solution:
Python 3 example:
numbers = [4, 8, 15, 16, 23]
total = 0
for number in numbers:
total = total + number
print(total)
print(total / len(numbers))
Expected output:
66
13.2
The running totals are 4, 12, 27, 43 and 66. An empty list would require a guard before dividing by its length.
Walkthrough: total = 0 is set once before the loop. Each pass adds one element. len(numbers) is used instead of hard-coding 5 so the code still works if the list changes.
Exam tip: Use len() rather than a literal count — it makes the program robust and shows good practice.
Example 4: Validation using a while loop (robust programs)
Question: Write a program that keeps asking for a password until it is at least 8 characters long.
Solution:
Python 3 example:
password = input("Password: ")
while len(password) < 8:
password = input("Use at least 8 characters: ")
print("Accepted")
Walkthrough: A condition-controlled while loop repeats while the rule is broken (len < 8). The re-prompt happens inside the loop, and the first input happens before it so the loop has something to test.
Test data: "abc" (invalid, too short — rejected), "password" (boundary, exactly 8 — accepted), "longpassword" (normal — accepted).
Exam tip: Loop while the input is invalid, accept when the condition becomes false. Get the first input before the loop, then re-ask inside it.
Example 5: Debugging — find and fix the errors
Question: This program should print the average of three numbers but is faulty. Identify the errors and give the corrected code.
Deliberately faulty Python — diagnose before running:
def average(a, b, c)
return a + b + c / 3
print(average(10, 20, 30))
Solution:
There are two faults:
- Syntax error: the function header is missing a colon (
:).
- Logic error: operator precedence means
a + b + c / 3 divides only c by 3, giving 10 + 20 + 10 = 40. Brackets are needed.
Corrected code:
Python 3 example:
def average(a, b, c):
return (a + b + c) / 3
print(average(10, 20, 30)) # 20.0
Walkthrough: The missing colon stops the program running at all (syntax). Even once it runs, the missing brackets give the wrong answer (logic). Note the question contains both error types — name each correctly.
Exam tip: When asked to "identify the type of error", use the precise term. A missing colon is syntax; a wrong calculation that still runs is logic.
Example 6: Trace table for an unseen algorithm
Question: Complete a trace table and state the output of this algorithm when x = 13.
Python 3 example:
x = 13
count = 0
while x != 1:
if x % 2 == 0:
x = x // 2
else:
x = 3 * x + 1
count = count + 1
print(count) # 9
Each trace row below shows x before the update and count after it. This example terminates for 13; it is not a proof that this process terminates for every positive integer.
Solution:
| x | x MOD 2 | count | Note |
|---|
| 13 | 1 (odd) | 1 | x = 3*13+1 = 40 |
| 40 | 0 (even) | 2 | x = 40 DIV 2 = 20 |
| 20 | 0 (even) | 3 | x = 10 |
| 10 | 0 (even) | 4 | x = 5 |
| 5 | 1 (odd) | 5 | x = 16 |
| 16 | 0 (even) | 6 | x = 8 |
| 8 | 0 (even) | 7 | x = 4 |
| 4 | 0 (even) | 8 | x = 2 |
| 2 | 0 (even) | 9 | x = 1, loop ends |
Output: 9
Walkthrough: Each iteration applies MOD to test odd/even, updates x with either DIV 2 or 3x+1, and increments count. The loop stops when x reaches 1. Filling one row per change makes the count easy to follow.
Exam tip: For MOD/DIV loops, write the test result (odd/even) in its own column — it stops you applying the wrong branch and keeps your trace accurate.