Skip to main content

Chapter 6b - Break

break leaves a loop immediately.

for i in range(100):
print(i)
if i == 3:
break
πŸ§ͺ Try it

Mini quiz β€” leaving a loop​

Quiz

Leaving a loop

1 / 2

After break, what happens to the remaining loop rounds?

for i in range(100):
print(i)
if i == 3:
break

Pick an answer to see if you are right.

Challenge: stop at eight​

Write up_to_stop(stop) so it prints 0 then 1 then ... then stop, each on its own line, then stops (do not print numbers after stop).

Expected for 2:

0
1
2

Mini quiz β€” break​

Quiz

break

1 / 1

What does break do?

Pick an answer to see if you are right.

Exercises​

Activity: Find yes

You search tunnels. Each answer is yes or no on one line (space-separated). Write tunnels_until_yes(words) so it returns how many tunnels you entered including the one with yes. If there is no yes, return the length of the list.

no no yes no β†’ 3