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

Bank Raid - I need help!

this is 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 = coinIndex
# Collect that coin.
hero.moveXY(coin.pos.x, coin.pos.y)
# Increase coinIndex by one.
coinIndex += 1

What did I do wrong?

Howdy and welcome to the forum!

Please post your code properly formatted…we need to be sure that the proper indentations are being used, as this is also part of the code syntax. You can learn how to do this here: [Essentials] How To Post/Format Your Code Correctly

One detail I can see that needs correcting is how you assigned the coin variable value, review how the enemy variable was declared above.

Was there an error shown? If so, always provide those details to help others debug. Also giving a short description of what is happening (even when it is not what you want) may help too especially later with longer code.

um, not quite.

you’ll need to bress the button that looks like this: </>
where it says preformatted text you’ll need to put your code.

There we go! Thanks @ TheBredMeister

# 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 = coinIndex
        # Collect that coin.
        hero.moveXY(coin.pos.x, coin.pos.y)
        # Increase coinIndex by one.
        coinIndex += 1

Line 19…coinIndex is a counter, which is used to indicate which element of the coins array the code is working on.

My hero attacks the enemy, but doesn’t get the code.

Oops meant coins :smile:

ok I found the issue.

It needs to be coins = hero.findItems()

the rest is good though.

Still not working…

Found another issue actually,

you wrote coin = coinIndex when it should be coin = coins[coinIndex]

Have you fixed line 19? You had it as:

coin = coinIndex

Which is saying coin = 0 on the first iteration, then 1 on the second, and so on.

Instead, you need to define coin as a specific element of the coins array, by using coinIndex

Just found that one my self. It still doesn’t work.

Your code works for me.
What equipment do you have?

I copied that code and it completes just fine…you got it!

Thanks for the help.

1 Like

Welcome! Please edit the previous posts and remove the code…that would be considered a solution, which any can copy and pass the level with.

No problem. Arrays can be tricky, but you’ll get it down!

Now slay them munchkins and make us proud!