[Solved] Weakest Quickest

Just starting up playing again after a lengthy layoff and this level is kicking my butt. I’m sure that I can’t see the forest through the trees and I’m just overlooking the obvious, but I’ve tried about 30 different combinations of everything I can think of and it’s just not working. I’m pretty sure that the problem is in the function.

# Defeat shamans to survive.
# The function find the weakest enemy.
def findWeakestEnemy():
    enemies = hero.findEnemies()
    weakest = None
    leastHealth = 99999
    enemyIndex = 0
    # Loop through enemies:
    while enemyIndex < len(enemies):
        enemy = enemies[enemyIndex]
        # If an enemy's health is less than leastHealth:
        if enemy.health < leastHealth:
            # Make it the weakest
            weakest = enemy
            # and set leastHealth to its health.
            leastHealth = enemy.health
        return weakest
        

while True:
    # Find the weakest enemy with the function:
    weakest = findWeakestEnemy()
    # If the weakest enemy here:
    if weakest:
        # Attack it!
        hero.attack(weakest)
    pass

You are not incrementing your index.

1 Like

THANK YOU! THAT WORKS!

I’ve been banging my head on my desk since yesterday over this.

Here’s a donut just for you :doughnut:

Youre Welcome! :grinning:

I keep getting wasted and I can’t work out why here is my code in javascript

function findWeakestEnemy() {
var enemies = hero.findEnemies();
var weakest = null;
var leastHealth = 99999;
var enemyIndex = 0;
// Loop through enemies:
while(enemyIndex < enemies.length) {
    var enemy = enemies[enemyIndex];
    // If an enemy's health is less than leastHealth:
    if(enemy.health < leastHealth) {
        // Make it the weakest 
        weakest = enemy;
        leastHealth = enemy.health;
        // and set leastHealth to its health.
        return weakest;
    }
      enemyIndex += 1;
}   

}

while(true) {
    // Find the weakest enemy with the function:
    var weakest = findWeakestEnemy();
    // If the weakest enemy here:
    if (weakest) {
        // Attack it!
        hero.attack(weakest);
    }
}

Please format your code properly, as it says in the FAQ.
thanks

Sorry its my first post

You misplaced
return weakest;

Got it thanks I fixed it and passed the level :slight_smile:

It’s absolutely fine, It’s a common mistake that almost everyone (including me) does the first time they post code. :grin: