Skip to content
Aabha AI Academy
Skip to reading

Python · Module 2

Answers and explanations · Decisions and repetition

Compare these references after your own attempt. You can read them at any time; they do not record a live model or vendor run.

Module 2

Check negative and zero before the short-session branch so categories do not overlap. Initialize the accumulator outside the loop. The countdown decreases until its condition is false.

Download the complete answer

# Reference answer. Attempt the exercise first.
minutes = 30
if minutes < 0:
    label = "invalid"
elif minutes == 0:
    label = "rest"
elif minutes < 30:
    label = "short"
else:
    label = "long"
total = 0
for value in [10, -5, 0, 25]:
    if value > 0:
        total += value
remaining = 3
countdown = []
while remaining > 0:
    countdown.append(remaining)
    remaining -= 1
assert label == "long" and total == 35 and countdown == [3, 2, 1]
print("Module 2 checks passed")

Exit-check guide: The trace totals are 10, 10, 10, 35. Empty and all-negative inputs total zero. range(2,5) ends at 4. The input 30 distinguishes > from >=.

Return to this module