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β
Leaving a loop
1 / 2After 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β
break
1 / 1What 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