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

Clash of the Clones Python

I am having trouble with my code on this level. I am trying to find the weakest enemy and then attack that enemy but nothing is happening. PLS HELP!

# This function should go through the enemy array and find the weakest enemy
def findWeakestEnemy():
    enemies = hero.findEnemies()
    weakest = None
    leastHealth = 99999
    enemyIndex = 0
    # Picking out individual enemy from the array and testing his health agains leastHealth
    if enemyIndex < len(enemies):
        enemy = enemies[enemyIndex]
        if enemy.health < leastHealth:
            weakest = enemy
            leastHealth = enemy.health
            enemyIndex += 1
            return


while True:
    enemy = findWeakestEnemy()
    if enemy:
        hero.attack(enemy)

Howdy and welcome to the forum! Please repost your code, properly formatted. You can learn how here: [Essentials] How To Post/Format Your Code Correctly

Hi dedreous,

Thanks for the tip. I’m sure you have to say that hundreds of times a day!

Heh…unfortunately 'tis true. :slight_smile:

The first issue I see is with:

    if enemyIndex < len(enemies):

Rather than using an ‘if’ statement, you should use a conditional loop instead. As written now, ‘enemyIndex’ will eventually become >= len(enemies), since you are incrementing it, so eventually, this entire ‘if’ block with be ignored.