

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
No active XP Potion
Accept a Quest
Login to submit answers
Back
ctrl+,
Next
ctrl+.
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.
Focus Editor
Alt+Shift+]
Next Tab
Alt+Shift+[
Become a member to Submit
Become a member to Run
Become a member to view solution