[Help] Clash of the Clones

So I have slowly gotten this down and I can’t figure this out at all. I’ve gotten down so that I seem to be able to attack most of the guys and not have much problem, but then my guy keeps running off towards sand yaks and I’ve tried a dozen different ways to exclude sand yaks and several of them caused problem. Please help.

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
        enemyIndex += 1
        return weakest

while True:
    # Find the weakest enemy with the function:
    enemy = hero.findEnemies()
    weakest = findWeakestEnemy()
    distance = hero.distanceTo(weakest)
    if enemy.type != "sand-yak":
        if hero.canCast("chain-lightning", weakest):
            hero.cast("chain-lightning", weakest)
        if hero.isReady("stomp") and distance > 10:
            hero.stomp(weakest)
        if hero.isReady("bash") and distance > 10:
            hero.bash(weakest)
        elif weakest:
            hero.attack(weakest)
    pass

fixed code and indents. Helps if you use the right button.

Hi, @smijas, welcome to the codecombat discourse.
To format your code you need to put ``` in at the start and end of your code. I did it this time.
Maybe you could try if enemy.health < leastHealth and enemy.type != ‘sand-yak’.
That might work.

Thanks. I tried that but causes an issues with targeting since it makes “weakest” no longer an object, according to CodeCombat.

In your while True loop, you are currently casting ‘enemy’ as an object containing enemies…this is actually an array of enemy objects. Which object (element) is ‘enemy.type’ pointing to?

I am not sure I understand the question.

No worries…I just realized that I’m not talking about the error you received, but the code you originally posted. So, taking only part of your code:

while True:
    # Find the weakest enemy with the function:
    enemy = hero.findEnemies()
    weakest = findWeakestEnemy()
    distance = hero.distanceTo(weakest)
    if enemy.type != "sand-yak":

You are defining enemy as ‘all enemies’, thereby making it an array of objects (enemies). In your if statement, you are testing to see if the ‘enemy’ type is not equal to ‘sand-yak’.

Which enemy is this testing tho? So far, ‘enemy’ is a list of many, but you are attempting to test for a single…

Oh yeah. I understand now.

I tried several different definitions of enemy. I tried a specific enemy with hero.findNearestEnemy and it didn’t work either.

I’ve tried a few different ideas, this just happens to be the code when I finally got frustrated and can’t figure out what I’m doing wrong.

As Danny suggested, adding the additional qualifier to your if statement might help. Just a guess, but I think you got that error because the target either died, or otherwise got misconstrued, hence the ‘does the target exist?’ part of the message.

Finally got that to work, but that didn’t actually solve the level and I kept playing to make it work. Nothing did.
I finally just switched to a Flag method and solved it that way.