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

I solved the problem but i didnt understand the code

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 = enemies[enemyIndex]
        # If enemy.health is greater than strongestHealth
        if enemy.health > strongestHealth:
            # Set `strongest` to enemy
            # Set strongestHealth to enemy.health
            strongest = enemy
            strongestHealth = enemy.health
        # Increment enemyIndex
        enemyIndex += 1

    return strongest

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

So code says if enemy’s health > strongest but strongest = 0 so every enemy strongest. I solved nearly 100 level, but level has guide to say what i do. So problem is i solve problem without understand, not all of them but specially levels that has def command

Yusuf, please be sure to properly format your code when you post it. You can read how to do so here: [Essentials] How To Post/Format Your Code Correctly

However, to speak to your question. Yes, strongest = 0, but only at the start. The first enemy you encounter will replace the zero with their health value. Now, the next enemy is tested. If it is stronger, then it replaces the prior enemy; but if it is weaker, the code will skip him and move on to the next…and so on. In the end, you end up with the leader, who just happens to be the strongest of all.

1 Like

[en_US.composer.my_button_text]