

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Difficulty: 8
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
Sometimes, while looping through a sequence, you may find items that you want to skip. Python (like many programming languages) provides a way to do this: the continue statement.
continue means "go directly to the next iteration of this loop." Whatever else was supposed to happen in the current iteration is skipped.
Let's say we want to print all the numbers from 1 to 50, but skip every 7th number. We can use continue to do this, by keeping track of a counter:
# Remember, `range` is inclusive of the start, but exclusive of the end
counter = 0
for number in range(1, 51):
counter = counter + 1
if counter == 7:
counter = 0 # Reset the counter
continue # Skip this number
print(number)
What we'll see printed are all the numbers from 1 to 50, except for 7, 14, 21, 28, 35, 42, and 49.
A continue statement immediately halts the current iteration and jumps to the next one, which saves the program from doing unnecessary work.
For example, if we're calculating square roots, we might want to skip negative numbers. continue lets us move on to the next number without wasting any time:
for number in range(-5, 5):
if number < 0:
continue # Skip negatives
print(f"The square root of {number} is {number**0.5}")
This would print:
The square root of 0 is 0.0
The square root of 1 is 1.0
The square root of 2 is 1.4142135623730951
The square root of 3 is 1.7320508075688772
The square root of 4 is 2.0
Using continue to avoid pointless work can make your code run faster, which is especially helpful if the loop includes time-consuming computations.
In Fantasy Quest, we grant the player an enchantment for every three quests we track. The higher their total number of completed quests, the stronger the enchantment.
Fix the award_enchantments function. It calculates the strength of the enchantment – 5 times the total number of quests completed – and prints a message for the player. But we need to make sure this happens only once for every three quests that we iterate over within the loop! Only count the quests in the loop, don't worry about the total number of quests being divisible by 3.
Example:
award_enchantments(2, 8, 1)
# loop through the quests:
# 2 -> first quest: continue
# 3 -> second quest: continue
# 4 -> third quest: "Enchantment of strength 20 awarded for completing 4 quests!"
# 5 -> first quest: continue
# 6 -> second quest: continue
# 7 -> third quest: "Enchantment of strength 35 awarded for completing 7 quests!"