Play Home Contribute Github Join Us! Discourse Staff Members Github Contribution Guides Team

Help With Bank Raid

while True:
    enemies = hero.findEnemies()
    enemyIndex = 0
    while enemyIndex < len(enemies):
        enemy = enemies[enemyIndex]
        hero.attack(enemy)
        enemyIndex += 1
    coins = hero.findItems()
    coinIndex = 0
    while coinIndex < len(coins):
        coin = coins[coinIndex]
        hero.moveXY(coin.pos.x, coin.pos.y)
        coinIndex = coinIndex + 1

This is my code and after defeating the first wave of ogres, he just stands there saying “its dead!”

Maybe put a while loop in so that you only attack the enemy while the enemy.health is greater than 0?

use a for loop instead.

for enemy in enemies:
    while enemy.health > 0:
        self.attack(enemy)

# For loop doesn't require indexs

sorry for not replying i was super busy finishing school

At this stage in the desert you haven’t come across for yet. It’s important for people to know how to use both, so don’t tell people to use a for loop instead of a while loop.
Danny

1 Like

i learned for loops from my teacher 2 years ago, so thats why in desert i use for loops

I’m sure you did, but it’s important for everyone one to know how to do while loops. For loops are more complex (you’re using the simple type, which is less common in “real” programming than the complex one) so it’s good to leave it until later.
If you tell people to use for loops they might never learn while loops properly, it’s also just confusing. So don’t. :smile:
Danny