# Mountain Mercenaries/ Python/ Help [Solved]

I can’t command the soldiers to attack

``````while True:
# Move to the nearest coin.
coin = hero.findNearestItem()
soldiers = hero.findFriends()
soldierIndex = 0
soldier = soldiers[soldierIndex]
enemy = hero.findNearestEnemy
# Use move instead of moveXY so you can command constantly.
hero.move(coin.pos)
# If you have funds for a soldier, summon one.
if hero.gold > hero.costOf("soldier"):
hero.summon("soldier")
soldierIndex += 1
soldiers = hero.findFriends()
soldierIndex = 0

# Loop over all your soldiers and order them to attack.
if soldierIndex >= 1:
hero.command(soldier, "attack", enemy)
soldier = soldiers[soldierIndex]
solierIndex -= 1
# Use the 'attack' command to make your soldiers attack.
#hero.command(soldier, "attack", enemy)\`````````

No need for the soldierIndex, or the `soldier = soldiers[soldierIndex]`. In your if loop, summon a soldier, that’s all. Then have a for loop:

``````for soldier in soldiers:
``````

Then command soldier to attack enemy if the enemy exists. For your coins, if there is a coin, move to the coin’s position.

1 Like
``````while True:
# Move to the nearest coin.
coin = hero.findNearestItem()
soldiers = hero.findFriends()
soldierIndex = 0
soldier = soldiers[soldierIndex]
enemy = hero.findNearestEnemy
# Use move instead of moveXY so you can command constantly.
hero.move(coin.pos)
# If you have funds for a soldier, summon one.
if hero.gold > hero.costOf("soldier"):
hero.summon("soldier")
soldierIndex += 1
soldiers = hero.findFriends()
soldierIndex = 0

# Loop over all your soldiers and order them to attack.
for soldier in soldiers:
hero.command(soldier, "attack", enemy)

# Use the 'attack' command to make your soldiers attack.
#hero.command(soldier, "attack", enemy)
``````

like this?

this

and this

delete them then put that into a if statement.

1 Like

wait i found out what i did wrong thank you

So what is the problem now?

1 Like

It works now i fixed it

Great can you change the category to #level-help and mark whichever post helped solve your problem.

And a big congrats on finishing the level. I made the exact same mistake as you. Don’t worry you’ll get the hang of it soon enough .

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.