until: stop when it is True
Repeat until the glass is full means: keep pouring while it is not full yet.
05LOOPS · REPEAT UNTIL
Some languages, like Scratch, have a repeat until block. Python has no until word. Here are two ways to do it anyway.
LOOK FIRST
Repeat until the glass is full means: keep pouring while it is not full yet.
not True is False. not False is True.
There is no until word in Python. Write while not, or while True with break.
while not full:pourpourpourfull!WAY 1
Read while not full as repeat until full.
full = FalseA yes-or-no box. The glass is not full yet.cups = 0No cups poured yet.while not full:Repeat until full. not False is True, so a turn starts. cups = cups + 1Pour one more cup. print("pour", cups)Shows pour 1, pour 2, pour 3. if cups > 2:After the third cup, the glass is full... full = True...so the box changes. not True is False, and the loop stops.print("full!")Runs once, after the loop.WAY 2
The loop would run forever, so you add a door out: break.
cups = 0No cups poured yet.while True:True is always True, so on its own this loop never stops. cups = cups + 1Pour one more cup. print("pour", cups)Shows pour 1, pour 2, pour 3. if cups > 2:Is the glass full now? breakbreak means: leave the loop now. Python jumps to the first line after it.print("full!")Runs once, after the loop.LOOK FIRST
With while True and break, the job runs at least once, then PyBot checks.
while not done means repeat until done is True.
Use break inside a loop, usually inside an if.
RUN IT
Guess first. Then run real Python and check.
jumps = 0
while True:
jumps = jumps + 1
print("jump")
if jumps > 3:
breakHow many times will PyBot jump?
PyBot's hint
Ready. Press Run Python.
Pyodide 314.0.3 runs CPython in your browser with WebAssembly.
Open-source under the Mozilla Public License 2.0. License
Now change one thing: Change 3 to 5. Run again and count the jumps.
YOUR TURN
Try each tiny question. A wrong answer becomes something to review.
while not done:What does this line mean?
not FalseWhat is not False?
while True:
print("hi")
break
print("bye")How many times does hi show?
done = True
while not done:
print("work")
print("rest")How many times does work show?
stars = 0
while not stars > 2:
stars = stars + 1
print(stars)What does Python show?
for while untilWhich word is NOT a Python word?
PyBot stops after one cup, but the glass holds 3. Fix the question in the if line.
pour 1 pour 2 pour 3 full!OUTPUT
Want a fresh start? Clear only this page.
Next, you will walk through a word, one letter at a time.