PythiloLesson 11 of 30 · progress kept in this browser

break, continue and else

Inside any loop, break leaves the loop at once and continue skips the rest of the current pass. A loop can also end with an else block, which runs only if the loop finished without a break.

for n in [4, 9, 15, 21]:
    if n % 2 == 0:
        continue          # skip evens
    if n > 10:
        print("stopped at", n)
        break
    print("odd", n)
else:
    print("not printed: break ran")
Your task

Write total_until_stop(numbers) that adds the numbers in order, skips negative ones, and stops at the first 0. Write smallest_divisor(n) for n of at least 2, returning the smallest divisor above 1 (which is n itself for a prime), trying d only while d * d <= n.

Next: Functions