←Previous page

05LOOPS · REPEAT UNTIL

Repeat until it is done.

Some languages, like Scratch, have a repeat until block. Python has no until word. Here are two ways to do it anyway.

LOOK FIRST

until is while turned around.

until: stop when it is True

Repeat until the glass is full means: keep pouring while it is not full yet.

not flips the answer

not True is False. not False is True.

Python has no until

There is no until word in Python. Write while not, or while True with break.

while not full:pourpourpourfull!

WAY 1

while not: repeat until a box is True.

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

while True with break: stop from inside.

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

Three things to know about repeat until.

The job runs first

With while True and break, the job runs at least once, then PyBot checks.

while not = until

while not done means repeat until done is True.

break leaves the loop

Use break inside a loop, usually inside an if.

RUN IT

Help PyBot jump until it is tired.

Guess first. Then run real Python and check.

1

Guess the result

jumps = 0 while True: jumps = jumps + 1 print("jump") if jumps > 3: break

How many times will PyBot jump?

jumps.pyREAL PYTHON
OUTPUT
Ready. Press Run Python.
Python runs here thanks to Pyodide ♥

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

Repeat until it is done.

Try each tiny question. A wrong answer becomes something to review.

0Completed
8Not tried
0Review
01

Read it out loud

while not done:

What does this line mean?

02

Flip it

not False

What is not False?

03

What break does

while True: print("hi") break print("bye")

How many times does hi show?

04

Already done

done = True while not done: print("work") print("rest")

How many times does work show?

05

Until there are enough

stars = 0 while not stars > 2: stars = stars + 1 print(stars)

What does Python show?

06

Python's loop words

for while until

Which word is NOT a Python word?

07

Fix PyBot's code

PyBot stops after one cup, but the glass holds 3. Fix the question in the if line.

Goal output
pour 1
pour 2
pour 3
full!
OUTPUT

Your work is saved here.

Want a fresh start? Clear only this page.

!
BIG IDEA

To repeat until, write while not, or while True with break.

Next, you will walk through a word, one letter at a time.