break and continue
Python · Unit 3: Loops
- Full plan
- Python
- 8 exercises
break leaves the loop right then, even if rounds are left.
The program carries on with whatever comes after the loop.
for n in range(1, 10):
if n == 4:
break
print(n)
print("Done")Prints
1 2 3 Done
The rest of the explanation is in the lesson, which is part of the full plan.
Exercises in this lesson
You do them in the app, which checks them on the spot and explains why.
1. Predict the output
What does this code print?
2. Multiple choice
What does
breakdo?3. Predict the output
What does this code print?
4. Complete the code
Complete this to print 1 to 7, skipping the multiples of 3.
5. Predict the output
This code looks for the first number whose square goes past 50. What does it print?
6. Find the bug
This program should print only the first multiple of 7 greater than 30. Which line has the error?
7. Put the lines in order
The person types prices and types done at the end. Put the lines in order to add up the prices.
8. Predict the output
In this game you have to guess the number. The person types 3, then 9 and then 7. What does it print?
It opens in your browser. This lesson is part of the full plan; the first unit of each course is free.