```````https://codecombat.com/play/level/mad-maxer?
Can someone help me? Thanks!
# Attack the enemy that's farthest away first.

while True:
farthest = None
maxDistance = 0
enemyIndex = 0
enemy = hero.findNearestEnemy()
# Look at all the enemies to figure out which one is farthest away
while enemyIndex < len(enemy):
target = enemy[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.
while enemy.health > 0:
hero.attack(enemy)`
``````

Hello and welcome to codecombat discourse! This is a cozy forum where you can share ideas, share fan art, get assistance for code, etc! Before you proceed, we hope that you review this topic, which shows all essentials of this board! Thanks!

Huh. @Anonym Why is your @ not @ing him?

idk, it didnâ€™t work occasionally

did I do something wrong?

I did it so now what am I supposed to do.

I am very sorry, but I canâ€™t reply now(((((
@Eric_Tang ?

I have school right now.

Itâ€™s because this part is not inside of the while loop checking if enemyIndex is less than the length of enemies. Indent it one time.

Here you want to check if farthest health is greater than zero. If it is, then attack farthest.

I do not understandâ€¦

For the second part I mentioned, if you are attacking enemy, then you will be attacking the nearest enemy. You need to attack the farthest enemy not the nearest enemy. So you need attack farthest while farthest health is greater than zero. But if you attack farthest and do indent the first part I mentioned, farthest wonâ€™t update, so you wonâ€™t attack farthest. So all you need to do is restart your code, and then you need to add this in the if farthest loop:

``````while farthest.health >0:
hero.attack(farthest)
``````
1 Like

Thank you so much! you are awesome!

2 Likes

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.