# Attack the enemy that's farthest away first.
while True:
farthest = None
maxDistance = 0
enemyIndex = 0
enemies = hero.findEnemies()
# Look at all the enemies to figure out which one is farthest away.
while enemyIndex < len(enemies):
target = enemies[enemyIndex]
enemyIndex += 1
# Is this enemy farther than the farthest we've seen so far?
distance = hero.distanceTo(target)
if distance > maxDistance:
maxDistance = distance
farthest = target
if farthest:
# Take out the farthest enemy!
# Keep attacking the enemy while its health is greater than 0.
hero.attack(farthest)
pass
What problem/error are you seeing? Off the top, it looks like you are only attacking the enemy once…you should add a while loop that keeps attacking until it is dead.
# Attack the enemy that's farthest away first.
while True:
farthest = None
maxDistance = 0
enemyIndex = 0
enemies = hero.findEnemies()
# Look at all the enemies to figure out which one is farthest away.
while enemyIndex < len(enemies):
target = enemies[enemyIndex]
enemyIndex += 1
# Is this enemy farther than the farthest we've seen so far?
distance = hero.distanceTo(target)
if distance > maxDistance:
maxDistance = distance
farthest = target
if farthest and farthest.health > 0:
# Take out the farthest enemy!
# Keep attacking the enemy while its health is greater than 0.
hero.attack(farthest)
pass
And, you are right…a while loop is a while loop. However, their importance and/or behavior can be greatly impacted by where they are placed. A single block of code could contain many loops…the first/outer loop is usually the main ‘true loop’ (sometimes not always tho). But inside that main loop, you can nest other loops that control specific, smaller, blocks of code.
As Rheat mentioned:
is one such embedded loop.
You can also step blocks of code by using loops. These are different from nested loops, in that the first step must complete, before the the next one will run. Kind of like:
firstStep == "notDone"
while True:
while firstStep == "notDone":
# do things to complete first step
firstStep == "done"
secondStep == "notDone"
while secondStep == "notDone":
# do second step
secondStep == "done"
Honestly, both of those loops are nested inside the outer ‘true’ loop, but they do (I hope) help to show the concept of stepping.