Play Home Contribute GitHub Join Us Discourse Staff Members GitHub Contribution Guides Team

[SOLVED]Problem with function


When i run my code it shows this error.

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

while True:
    # Find the weakest enemy with the function:
    # If the weakest enemy here:
    if weakest:
        # Attack it!


You do not need to put enemy[enemyIndex] when you redefine weakest, just enemy. Same deal with the lowest health.
Also it is weakest = enemy, not enemy = weakest.
Happy to help :slightly_smiling_face:


Make sure you assign findWeakestEnemy to a variable; For example:

weakestEnemy = findWeakestEnemy()

if weakestEnemy:


Now i’m getting this


Try leastHealth =


Now it works! Thanks


Happy to help, @Szymon859! :slightly_smiling_face:


Nice Job @Hellenar. I did not see that!