

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Difficulty: 5
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
Python has another type of loop, the while loop. It's a loop that continues while a condition remains True. The syntax is simple:
while 1:
print("1 evaluates to True")
# prints:
# 1 evaluates to True
# 1 evaluates to True
# (...continuing)
The example above is hardcoded to continue forever, creating an infinite loop. Typically, a while loop condition is a comparison or variable, and it determines when the loop ends:
num = 0
while num < 3:
num += 1
print(num)
# prints:
# 1
# 2
# 3
# (the loop stops when num >= 3)
In Fantasy Quest, player characters regenerate health when standing still while away from enemies. This means they will gain health but can't run from enemies that are coming towards them while regenerating.
Complete the regenerate function using a while loop. It takes current_health, max_health and enemy_distance integers and returns an integer.
Ensure that the return statement is placed outside the while loop to correctly return the final value after the looping ends.