Mountain Mercenaries help

Hy guy, i try to complete this level, my summon.soldier attack enemy, but dead without defeat any enemy… this is my code.

while True:
    # Move to the nearest coin.
    # Use move instead of moveXY so you can command constantly.
    coin = hero.findNearestItem()
    hero.moveXY(coin.pos.x, coin.pos.y)
    
    if hero.gold > hero.costOf("soldier"):
        hero.summon("soldier")
    
    enemy = hero.findNearest(hero.findEnemies())
    if enemy:
        soldiers = hero.findFriends()
        soldierIndex = 0
        soldier = soldiers[soldierIndex]
        
        while soldierIndex < len (soldiers) :
            hero.command(soldier, "attack", enemy)
            soldierIndex +=1

I can see one problem straight away:

~~‘Use move instead of moveXY so you can command constantly.’ This line is important. Read your code carefully and you’ll find the mistake.
Also, make sure you have boots with the move (not moveXY) ability: fine boots, compound boots, boots of jumping, boots of leaping etc…
:lion: :lion: :lion:

Another specific detail that is also causing problems. The starter code assigns soldier before your while loop so you aren’t commanding every soldier, only the first one. Move the soldier line into the while loop before commanding them to run through all soldiers.

        soldier = soldiers[soldierIndex]
        
        while soldierIndex < len (soldiers) :
            hero.command(soldier, "attack", enemy)
            soldierIndex +=1

Ok… sorry, my english is very bad :slight_smile:

    coin = hero.findNearestItem()
    hero.move(coin.pos)

Thanks, i move the soldier line and the problem is solved… :slight_smile:

        while soldierIndex < len (soldiers) :
            soldier = soldiers[soldierIndex]
            hero.command(soldier, "attack", enemy)
            soldierIndex +=1

Don’t worry it’s doesn’t matter. :smile: Though I would recommend making sure you check for small differences like that, because a major part of coding is accuracy and exactness. e.g. If you don’t put a colon at the end of an if statement, it won’t work.
:lion: :lion: :lion:

1 Like