Decisions and Repetition
Conditions choose a path
Section titled “Conditions choose a path”score = 17
if score >= 20: result = "high"elif score >= 10: result = "medium"else: result = "low"
print(result)Python tests from top to bottom and executes the first matching branch. Indentation defines which instructions belong to each branch.
Predict results for 9, 10, 19, and 20. Values at the edges deserve attention because < and <= disagree exactly there.
Loops repeat while state changes
Section titled “Loops repeat while state changes”total = 0
for value in [3, 5, 7]: total = total + value print(value, total)Trace a table with iteration, value, and total. The final answer matters less than explaining every transition.
A while loop repeats while its condition remains true:
remaining = 3while remaining > 0: print(remaining) remaining -= 1Removing the final line creates a loop whose controlling state never approaches termination. Stop a runaway terminal program with Ctrl+C and read the resulting KeyboardInterrupt.
Knowledge check
Question 1 of 2
Knowledge check complete
You answered all 2 questions correctly.
What this gives us as researchers
Section titled “What this gives us as researchers”You can turn control flow into testable paths and trace state across repetitions. The next lesson packages behavior into functions and moves data through files.