Decisions and Repetition
What this concept is
An if selects a branch based on a Boolean condition. A for loop visits each item in a sequence. Indentation defines which instructions belong to a branch or loop. A loop's state includes the current item and any running totals.
Why it matters here
The tracker must reject unreasonable session lengths and combine any number of sessions, including none.
Concrete example
sessions = [25, 15, 20]
total = 0
for minutes in sessions:
if minutes < 1 or minutes > 240:
raise ValueError("Session outside allowed range")
total = total + minutes
print(total)
The running totals are 25, 40, 60. For an empty list the loop body never runs, so the total remains 0. The expression using or rejects either invalid boundary. raise stops normal execution and reports an error.
Choose a branch deliberately
minutes = 25
if minutes < 1 or minutes > 240:
print("Outside the allowed range")
elif minutes < 25:
print("Short session")
else:
print("Focused session")
The conditions are examined in order; only the first matching branch runs. Here the first condition is false and the second is false, so the else runs. Try 0, 1, 24, 25 and 241 on paper. Each indented group belongs to its preceding line ending in a colon. Use four spaces consistently; an unindented statement after this whole block runs regardless of which branch was selected.
The comparison operators are ==, !=, <, <=, > and >=. Inclusive bounds use <= or >=. “At least 25” includes 25; “more than 25” does not. For valid integer minutes, 1 <= minutes <= 240 expresses acceptance, while minutes < 1 or minutes > 240 expresses rejection. Using and in that rejection rule would require a number to be below 1 and above 240 simultaneously.
Trace the state, not just the final answer
For the total example, distinguish the current item minutes from the accumulated total:
- Before the loop:
totalis 0; no item has been processed. - First visit:
minutesis 25; the old total is 0; the new total is 25. - Second visit:
minutesis 15; the old total is 25; the new total is 40. - Third visit:
minutesis 20; the old total is 40; the new total is 60. - After the loop: the unindented print runs once.
At each visit, the total represents the sum of all items processed so far. That sentence explains both the starting zero and the update. Move the print inside the loop to observe the intermediate totals, then move it back. Resetting total inside the loop would break the sentence.
When a condition controls repetition
remaining = 3
while remaining > 0:
print(remaining)
remaining -= 1
print("Done")
Predict 3, 2, 1, then Done, each on its own line. remaining -= 1 means subtract one and assign the result back. The condition is checked before every iteration, including the first. Starting at 0 skips the body. If the subtraction is removed, the condition never becomes false; stop an accidental infinite terminal run with Ctrl+C.
Use for to visit known items; use while when a changing condition determines when to stop. break ends the nearest loop immediately. continue skips the rest of its current iteration. Neither is needed for the basic total; add them only when the contract requires that behavior. In particular, silently skipping invalid sessions would change our rejection contract.
Common confusion / misconception
Initializing total = 0 inside the loop loses previous work. range(3) produces 0, 1, 2, not 1, 2, 3. and and or are not interchangeable. A while loop repeats while a condition holds; forgetting to update its state can make it run forever.
How to use it
Write down the state before the loop and after each iteration. Test the empty sequence and boundaries 1 and 240, then the rejected values 0 and 241. Prefer a for loop when traversing an existing list.
Check yourself
What happens with [25, 0, 20]? Answer: execution raises at 0, before adding the final 20. The partial local total must not be mistaken for a valid result.
Mini drill or application
Compute the largest session with a running longest value initially 0. Explain why 0 is appropriate for the empty case under this positive-minutes contract.
Read this only if stuck
Official source: Python tutorial. Read only the matching section after attempting the example. These lessons and exercises are original curriculum material.