

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Difficulty: 4
click for more info
Not enough gems
Cost: 6 gems
1: Loops
incomplete
2: Loops Practice
incomplete
3: Loops Practice
incomplete
4: Loop Review
incomplete
5: Loop Review
incomplete
6: Whitespace in Python
incomplete
7: Range Continued
incomplete
8: Sum Game
incomplete
9: Sum Game 2
incomplete
10: While
incomplete
11: Continue Statement
incomplete
12: Break Statement
incomplete
13: Match Countdown
incomplete
14: Experience Points
incomplete
15: Meditate
incomplete
Back
ctrl+,
Next
ctrl+.
This lesson's interactive features are locked, please to keep using them
We can use continue to skip to the next iteration in a loop, but what if we want to exit the loop entirely? That's where the break statement comes in.
for n in range(42):
print(f"{n} * {n} = {n * n}")
if n * n > 150:
break
# 0 * 0 = 0
# 1 * 1 = 1
# 2 * 2 = 4
# 3 * 3 = 9
# 4 * 4 = 16
# 5 * 5 = 25
# 6 * 6 = 36
# 7 * 7 = 49
# 8 * 8 = 64
# 9 * 9 = 81
# 10 * 10 = 100
# 11 * 11 = 121
# 12 * 12 = 144
# 13 * 13 = 169
This code would loop from 0 all the way to 41, but it actually exits early. Once n * n is greater than 150, the break statement executes, stopping the loop.
When a character in Fantasy Quest is attacked, the strength of the attack is compared to the strength of the character's defensive enchantments. If the character has any enchantment that is at least as strong as the attack, then the attack is blocked and the character takes no damage.
Fix the check_defense function.
It checks each defensive enchantment against the attack. If an enchantment is strong enough, it prints that the attack is blocked. In that case, the loop should stop instead of checking further... make sure that it does!