Weakest quickest help

My hero doesn’t attack:

# Defeat shamans to survive.

# This function finds the weakest enemy.
def findWeakestEnemy():
    enemies = hero.findEnemies()
    weakest = None
    leastHealth = 99999
    enemyIndex = 0
    # Loop through enemies:
    if enemy:
        # If an enemy's health is less than leastHealth:
        if enemy.health < leastHealth:
            # Make it the weakest 
            enemy = weakest
            # and set leastHealth to its health.
            leasethealth = enemy.health
    return weakest

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

You need to have a loop

what do you mean? where should I loop?

…Follow the comments

1 Like

You could either use a for-loop, probably the easiest option, or a while-loop.

enemy isn’t even defined, so the function currently is not able to return weakest, which is why your hero is doing nothing.

1 Like
def findWeakestEnemy():
    enemies = hero.findEnemies()
    weakest = None
    leastHealth = 99999
    enemyIndex = 0
    # Loop through enemies:
    for enemy in enemies:
        # If an enemy's health is less than leastHealth:
        if enemy.health < leastHealth:
            # Make it the weakest 
            enemy = weakest
            # and set leastHealth to its health.
            leastHealth = enemy.health
    return weakest

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

I define enemy:

for enemy in enemies:

Just replace it with:
if enemy and enemy.health < leastHealth:

it still doesn’t work… it doesn’t like

leastHealth = enemy.health

Here’s the problem. You’re redefining enemy as weakest, which is None. So leastHealth = enemy.health is really leastHealth = None.health. To fix that, just switch enemy and weakest. So weakest = enemy.

1 Like

I tried, I attack the middle health sorcerer, instead of the least health sorcerer. I don’t know why…

Hmm … maybe try submitting a couple of times. You also might want to wait a couple of seconds first, it could be that not all of the shamans had spawned.

they all spawn… I just attack whichever spawns first.

You should use the for i in range(len(enemies)): for loop.

it doesn’t work. I don’t know why.

Can you show us your new code?

# Defeat shamans to survive.

# This function finds the weakest enemy.
def findWeakestEnemy():
    enemies = hero.findEnemies()
    weakest = None
    leastHealth = 99999
    enemyIndex = 0
    # Loop through enemies:
    for enemy in enemies:
        # If an enemy's health is less than leastHealth:
        if enemy and 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:
    weakestShaman = findWeakestEnemy()
    # If the weakest enemy exists:
    if weakestShaman:
        # Attack it!
        hero.attack(weakestShaman)



I tried replacing the loop with what @PeterPalov suggested, but it cause my code to completely break, as I did not have a defined enemy.

Use one equal sign here instead of two, you are assigning and not comparing