Play Home Contribute Github Join Us! Discourse Staff Members Github Contribution Guides Team

[SOLVED] Britte Morale - hero doesn't say anything

hy guy… ive problem whith this level… my code is hero:


# This should return the enemy with the most health.
def findStrongestEnemy(enemies):
    strongest = None
    strongestHealth = 0
    enemyIndex = 0
    # While enemyIndex is less than the length of enemies:
    while enemyIndex <len(enemies):
        # Set an enemy variable to enemies[enemyIndex]
        enemy = hero.findNearestEnemy
        enemies[enemyIndex] = enemy
        # If enemy.health is greater than strongestHealth
        if enemy.health > strongestHealth:
            
            strongestHealth = enemy.health
            strongest = enemy
            # Set `strongest` to enemy
            # Set strongestHealth to enemy.health
            
        # Increment enemyIndex
        enemyIndex +=1

    return strongest

enemies = hero.findEnemies()
leader = findStrongestEnemy(enemies)
if leader:
    hero.say(leader)

Hi lollo,
your problem is in these lines:

First of all, why are you finding your nearest enemy? That will always be the same. You need the enemy variable to change so you can compare their strengths.
Instead do what it tells you to do:
“Set an enemy variable to enemies[enemyIndex]”
If you want to iterate through the list of enemies you’ll need to use enemies[enemyIndex], right now you’re doing it the other way around.
Apart from that your code works.
Happy Coding,
:lion: :lion: :lion:

3 Likes

Ah ok … tahnks so much Deadpool198 … works fine :slight_smile:

1 Like