Sequence and Selection
Construct Python programs whose instruction order and selection logic match the stated objective.
What you need to be able to do
- construct programs whose instructions execute in a correct sequence;
- explain how instruction order can affect functionality and output;
- construct Python selection using if, elif and else;
- use Boolean and relational operators to control program flow;
- recognize control-flow failures such as incorrect output, infinite loops and deadlock.
Rapid Recall Deck
Attempt each card first. If a term is new, flip and study it, then answer it again without looking. Mark only secure recall as “Know it”.
Sequence: order changes behaviour
Incorrect order
price = -20
price = price * 0.90
print(price)
if price < 0:
print("Invalid price")
Repair the sequence
Rewrite the code so an invalid price is not discounted or printed as a valid result.
price = -20
if price < 0:
print("Invalid price")
else:
price = price * 0.90
print(price)Choose exactly the branch that should run
score = 78
if score >= 90:
band = "A"
elif score >= 75:
band = "B"
elif score >= 60:
band = "C"
else:
band = "D"
print(band)Trace before revealing
Predict the printed output for score = 78. Then explain why the final elif score >= 60 branch does not also run.
The output is B. The first condition is False, the second is True, so the program assigns band = "B" and skips the remaining elif and else blocks.
Boolean and relational operators
Relational operators
< · <= · > · >= · == · !=
Each comparison produces a Boolean value.
Boolean operators
and requires both conditions to be True.
or requires at least one to be True.
not reverses a Boolean value.
Predict four conditions
Given age = 17, has_id = True, score = 82, predict each expression.
age >= 18score >= 75 and has_idage >= 18 or score > 80not has_id- False
- True
- True
- False
Build a complete selection structure
Construct the Python decision
Assume these variables already exist: is_staff, is_student, age, trained.
"Access granted" or "Access denied".if is_staff or (is_student and age >= 16 and trained):
print("Access granted")
else:
print("Access denied")Incorrect output, infinite loops and deadlock
Incorrect output
Instructions execute in an order that calculates or displays the wrong value.
Infinite loop
A repetition condition never becomes False, so the program does not terminate normally.
Deadlock
Two or more tasks wait indefinitely for one another or for resources held by one another.
Diagnose the failure type
Task A repeatedly checks while attempts < 3 but never changes attempts. Task B locks resource X then waits for Y, while another task locks Y then waits for X.
Task A: infinite loop; attempts must be updated so the condition can eventually become False. Task B: deadlock; each task waits for a resource held by the other, so neither can proceed. A consistent resource-acquisition order is one way to avoid this circular wait.
Teacher-created Paper 2 practice
1. Given temperature = 31 and raining = False, construct an if/elif/else structure that prints "Indoor" when it is raining, "Heat plan" when it is not raining and temperature is at least 30, otherwise "Outdoor".
2. Explain why checking an input for validity after using it in a calculation can produce incorrect output.
3. Given x = 5, state the Boolean value of x != 5 or x >= 10.
if raining: print("Indoor") elif temperature >= 30: print("Heat plan") else: print("Outdoor")- The calculation may use invalid data and produce a value before the program rejects the input. Validation should occur before the dependent calculation.
- False.
Finish the learning cycle
IA — main task
Review the IA plan and identify one place where the order of processing steps matters. Make sure the planned sequence is explicit enough that another programmer could follow it.
Syllabus — short retrieval
Redo the access-control construct on page 7 from memory. Then make one new test case that should be granted and one that should be denied.