Warm-up · Activity 1 of 7
Warm-up from B2.2: what does this print?
print(list(range(1, 10, 4)))// B2.3 · ~25 min · Beginner
After this lesson you can stop a loop early with break, skip a round with continue, use else on a loop to handle "nothing found", and hold a place with pass.
Warm-up · Activity 1 of 7
print(list(range(1, 10, 4)))Predict · Activity 2 of 7
for n in [3, 8, 5, 12, 7]:
if n > 10:
break
print(n, end=" ")Practice · Activity 3 of 7
for n in range(1, 8):
if n % 3 == 0:
____
print(n, end=" ")Practice · Activity 4 of 7
for drink in ["tea", "milk", "juice"]:
if drink == "coffee":
print("found")
break
else:
print("not found")Practice · Activity 5 of 7
Brain teaser · Activity 6 of 7
count = 0
for i in range(3):
for j in range(3):
if j == 1:
break
count = count + 1
print(count)Apply · Activity 7 of 7
Check your work against this list
Read the worked example, then write the exercises. Your code runs in your browser or on your computer and is never uploaded.
Worked example
A shop’s order list uses two markers: -1 is a cancelled order, and 0 marks the end of the day. The program skips cancelled orders with continue, stops at the end marker with break, and warns in the loop’s else if the marker is missing. A second loop searches for a large order and reports when there is none. Change the list and run it again.
main.py
# -1 is a cancelled order, 0 marks the end of the day.
orders = [120, -1, 45, 300, -1, 80, 0, 999]
total = 0
count = 0
for amount in orders:
if amount == -1:
continue # cancelled: go on with the next order
if amount == 0:
print("End of day reached")
break # 999 is never read
total = total + amount
count = count + 1
else:
print("Warning: no end-of-day marker")
print("Orders:", count, "Total:", total)
# Is there an order over 250? else answers "no".
for amount in orders:
if amount > 250:
print("Large order:", amount)
break
else:
print("No large orders")
Run it with
python main.pyOutput
End of day reached
Orders: 4 Total: 545
Large order: 300Tab indents and Shift+Tab outdents. To leave the editor with the keyboard, press Esc, then Tab.
The first run downloads Python for your browser (up to 6.5 MB) and keeps it cached. Your code stays on your device.
Exercise 1 of 2
The program reads lines until the user types done. Every other line is a whole number, except that empty lines are skipped. Print the sum of the numbers. Use while True: with break for done and continue for an empty line. For 5, 10 and done it prints 15.
Tab indents and Shift+Tab outdents. To leave the editor with the keyboard, press Esc, then Tab.
The first run downloads Python for your browser (up to 6.5 MB) and keeps it cached. Your code stays on your device.
Inside while True:, read one line per round and decide what to do with it.
Check for done first and break. Then check for an empty line and continue, before int(line) is ever called.
if line == "done": break, then if line == "": continue, then total = total + int(line).
One way to solve it. Yours can look different and still pass the checks.
total = 0
while True:
line = input()
if line == "done":
break
if line == "":
continue
total = total + int(line)
print(total)
Install Python 3.14 or newer. Save these files in one folder, open a terminal in that folder, and run the commands below.
main.py
total = 0
while True:
line = input()
# Stop at "done"; skip empty lines; add the rest.
break
print(total)
test_main.py
from learnrun import run_main
def total_for(lines):
return run_main(lines).strip()
def test_two_numbers():
"""5 and 10 add up to 15"""
got = total_for("5\n10\ndone\n")
assert got == "15", f"for 5, 10, done the program printed {got!r}, expected '15'"
def test_done_first():
"""done straight away prints 0"""
got = total_for("done\n")
assert got == "0", f"for done alone the program printed {got!r}, expected '0'"
def test_empty_lines_skipped():
"""Empty lines are skipped"""
got = total_for("3\n\n4\n\ndone\n")
assert got == "7", f"for 3, empty, 4, empty, done the program printed {got!r}, expected '7'"
On macOS and Linux, type python3 wherever these commands say python, as in the first lesson.
Run the program:
python main.pyRun the checks (needs learnrun.py in the same folder):
python learnrun.py testDownload learnrun.pyExercise 2 of 2
The program reads a whole number n of at least 2. Try every divisor from 2 to n - 1. At the first one that divides n, print divisible by and the divisor, such as divisible by 3, and stop. If none does, print prime, using else on the for loop.
Tab indents and Shift+Tab outdents. To leave the editor with the keyboard, press Esc, then Tab.
The first run downloads Python for your browser (up to 6.5 MB) and keeps it cached. Your code stays on your device.
for d in range(2, n): tries every divisor; n % d == 0 means d divides n.
After printing the divisor, break, so only the smallest one is reported.
Write else: print("prime") at the same indentation as for, not as the if.
One way to solve it. Yours can look different and still pass the checks.
n = int(input())
for d in range(2, n):
if n % d == 0:
print("divisible by", d)
break
else:
print("prime")
Install Python 3.14 or newer. Save these files in one folder, open a terminal in that folder, and run the commands below.
main.py
n = int(input())
# Try the divisors 2 to n - 1.
print("prime")
test_main.py
from learnrun import run_main
def check(n):
return run_main(f"{n}\n").strip()
def test_prime():
"""7 and 2 are prime"""
got = check(7), check(2)
assert got == ("prime", "prime"), f"for 7 and 2 the program printed {got!r}, expected ('prime', 'prime')"
def test_first_divisor():
"""9 is divisible by 3"""
got = check(9)
assert got == "divisible by 3", f"for 9 the program printed {got!r}, expected 'divisible by 3'"
def test_smallest_divisor_only():
"""12 reports only its smallest divisor, 2"""
got = check(12)
assert got == "divisible by 2", f"for 12 the program printed {got!r}, expected only 'divisible by 2' (did you break?)"
def test_larger():
"""91 is divisible by 7"""
got = check(91)
assert got == "divisible by 7", f"for 91 the program printed {got!r}, expected 'divisible by 7'"
On macOS and Linux, type python3 wherever these commands say python, as in the first lesson.
Run the program:
python main.pyRun the checks (needs learnrun.py in the same folder):
python learnrun.py testDownload learnrun.pyx = 5
if x > 3:
break
What Python prints
SyntaxError: 'break' outside loopWhy, and the fix
break and continue only make sense inside a for or while loop. An if on its own is not a loop, so there is nothing to break out of. If you wanted to skip the rest of the if, restructure it with else.
for n in range(3):
print("done")
What Python prints
IndentationError: expected an indented block after 'for' statement on line 1Why, and the fix
Every loop, if and else needs at least one indented statement. When the body is still to be written, put pass there: for n in range(3):, then pass indented under it. Python then runs the file and the loop simply does nothing.
for n in range(3):
print(n)
if n == 2:
continue
What Python prints
SyntaxError: 'continue' not properly in loopWhy, and the fix
The if is not indented under for, so it runs after the loop and its continue is outside any loop. Indentation decides what is inside the loop: indent the if by four spaces to make it part of the loop body.
Python in the browser: Pyodide 314.0.7, MPL-2.0. Licence and source
5 questions, no hints. Score 80% or more to complete the lesson.
Finish every activity above to unlock the exit ticket.