Borrowed Sword- I'm so lost

For this level, your hero doesn’t fight.

Command your archers to focus fire on the enemy with the most health!

while True:
    enemies = hero.findEnemies()
    friends = hero.findFriends()
    bestHealth = 0
    best = 0
    for i in range(len(friends)):
        friend = friends[i]
    
    for enemy in enemies:
        if enemies.health > bestHealth:
            bestHealth = enemies.health
            best = enemy
    
    if bestHealth:
        hero.command(friend, "attack", best)

Take a closer look at your for loop for the enemies and make sure you are using the correct variable to identify the health of each enemy. The last line of the for loop is correct best = enemy.

while True:
    enemies = hero.findEnemies()
    friends = hero.findFriends()
    bestHealth = 0
    best = 0
    for i in range(len(friends)):
        friend = friends[i]
    
    for enemy in enemies:
        if enemy.health > bestHealth:
            bestHealth = enemy.health
            best = enemy
    
    if bestHealth:
        hero.command(friend, "attack", best)

i’m assuming like this but now all the Yetis are alive and destroy me

Yes, that section looks better.

The key is to command every friend to attack the same best enemy. The order of the code makes a difference in how it is executed. A better arrangement might go like

  1. Identify best enemy
  2. command all friends to attack the same best enemy

Think about how you would need to arrange the code to command each friend to attack the same best enemy.

One last little note, it is better to check the same variable that you are attacking. While the code will run when you check bestHealth, it isn’t really looking for the enemy you plan to attack. In this case, they are tied together so it will effectively run, but later on it may get you into trouble when you check for one thing and attack another.

3 Likes

Ah I finally got it, I see what you mean with the order now.

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