Bank Raid (HELP) SoLvEd

I’m not sure what’s wrong with my code, can you help me?

Here’s my code:

while True:
    enemies = hero.findEnemies()
    # enemyIndex is used to iterate the enemies array.
    enemyIndex = 0
    # While enemyIndex is less than len(enemies)
    while enemyIndex < len(enemies):
        # Attack the enemy at enemyIndex
        enemy = enemies[enemyIndex]
        hero.attack(enemy)
        # Increase enemyIndex by one.
        enemyIndex += 1
    coins = hero.findItems()
    # coinIndex is used to iterate the coins array.
    coinIndex = 0
    while coinIndex < len(coins):
        # Get a coin from the coins array using coinIndex
        coinIndex = coin
        # Collect that coin.
        hero.moveXY(coin.pos.x, coin.pos.y)
        # Increase coinIndex by one.
        coinIndex += 1

You’re trying to find the nth coin in the coins array. Replace that with:

coin=coins[coinIndex]

This will find the nth number coin in the coins array.

ok, i fixed it but it still doesn’t work. Can you help me for the next step. Sorry I just never understood this level.:sweat_smile:

Here’s my code:

# Wait for ogres, defeat them and collect gold.

while True:
    enemies = hero.findEnemies()
    # enemyIndex is used to iterate the enemies array.
    enemyIndex = 0
    # While enemyIndex is less than len(enemies)
    while enemyIndex < len(enemies):
        # Attack the enemy at enemyIndex
        enemy = enemies[enemyIndex]
        hero.attack(enemy)
        # Increase enemyIndex by one.
        enemyIndex += 1
    coins = hero.findItems()
    # coinIndex is used to iterate the coins array.
    coinIndex = 0
    while coinIndex < len(coins):
        # Get a coin from the coins array using coinIndex
        coin=coins[coinIndex]
        # Collect that coin.
        hero.moveXY(coin.pos.x, coin.pos.y)
        # Increase coinIndex by one.
        coinIndex += 1

Does your hero say “BUT IT’S DEAD” if so, you’ll have to change it to only attack if there health is above zero or your hero will get stuck in that loop.
:lion: :lion: :lion:

Been a while I did not post

Here what I believe is the problem with that code: The enemy does not die in a single hit. Because of that: the index will iterate until it is > len(enemies) then hero will stop attacking, get the coins and start over.

Even though the code can work it is a bad design.
In this case: enemy should only iterate if it is dead not if you hit them once.

keep in consideration to have better code:
Remove while loops and for loop as much as you can.
Does not mean to remove all of them though (you can’t anyways)
You want to get the coins only if there is no enemies.

3 Likes

I got it!! Thank you, @Chaboi_3000, @Deadpool198, and @Gabriel_Dupuis! :relaxed: