Help, mad maxer[SOLVED]

`https://codecombat.com/play/level/mad-maxer?
Can someone help me? Thanks!
![image|690x318](upload://2TZbIj3iEAFcU9F1iiGIHmQEdX8.jpeg)
# 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!

Hello, @Gerald_Jaydn_Fernand! Please read the stuff above, and format your code properly.

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

idk, it didn’t work occasionally

did I do something wrong?

Please read this topic to format the code correctly.

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

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.