I post to many topics. Weakest Quickest help please! [Solved]

def findWeakestEnemy():
    enemies = hero.findEnemies()
    weakest = None
    leastHealth = 99999
    enemyIndex = 0
    while enemy.Index < len(enemies):
        enemy = enemies[enemyIndex]
        if enemies.health < leastHealth:
            weakest = enemy
            leastHealth = enemy.health
    return weakest

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

I am sooo confused. Please help!!!

  1. It’s ok to make topics because you’re actually using them for good reasons.
  2. Right now I can’t get on our computer to do CoCo, so I can’t help. Sorry.

I also tried

for enemy in enemies:

It just doesn’t work!

What is your problem or error?

My hero doesn’t move. Like, at all.

I mean, I guess you could try putting hero.attack(weakest)
But idk if that would even help.

I fixed the parenthesis problem at the bottom after the function and got rid of the period in between enemy and Index at the top.

Now it says it has an infinite loop.

Was the parenthesis problem what I suggested? If so, now that youve taken the period out and it still doesn’t work, put it back to hero.attack(enemy)

No it was to the

enemy = findWeakestEnemy()

it at first didn’t have parenthesis after it.

I like how you when I clicked on your picture it showed a summery of what you like and stuff. How do you do that?

Will you please post your revised code?

def findWeakestEnemy():
    enemies = hero.findEnemies()
    enemyIndex = 0
    weakest = None
    leastHealth = 99999
    
    while enemyIndex < len(enemies):
        enemy = enemies[enemyIndex]
        if enemies.health < leastHealth:
            weakest = enemy
            leastHealth = enemy.health
    return weakest

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

How should I solve the infinite loop problem?

First of all, I asked you to ignore what I said about switching around the enemyIndex = 0
Secondly, I’m comparing your code with mine in Brittle Morale. In Brittle morale you’re doing the exact same thing but with strongest health. You’re switching back and forth from weakest to least. I feel like you should just stick with weakest.

Oh, and enemies should just be enemy.

I might be wrong about the least health. It is a different level I’m comparing it to, after all. But I think it’s worth a try.

but sence I did this

enemies = hero.findEnemies()

instead of enemy. Shouldn’t i keep it like it was?

I honestly think it should be enemy = hero.findEnemies()

Hahaha. I did that and there were like, twenty errors poping up. :crazy_face:

1 Like