# Mountain Mercenaries Python

**URL:** <https://discourse.codecombat.com/t/mountain-mercenaries-python/21439>\
**Category:** Level Help\
**Created:** [May 6, 2020, 5:12pm UTC](https://discourse.codecombat.com/t/mountain-mercenaries-python/21439 "2020-05-06T17:12:15Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![smijas](https://avatars.discourse-cdn.com/v4/letter/s/958977/32.png) [@smijas](https://discourse.codecombat.com/u/smijas)\
**Post date:** [May 6, 2020, 5:12pm UTC](https://discourse.codecombat.com/t/mountain-mercenaries-python/21439/1 "2020-05-06T17:12:15Z")

</div>

```python
while True:
    # Move to the nearest coin.
    # Use move instead of moveXY so you can command constantly.
    coin = hero.findNearestItem()
    hero.move(coin.pos)
    
    # If you have funds for a soldier, summon one.
    if hero.gold > hero.costOf("soldier"):
        hero.summon("soldier")
        
    enemy = hero.findNearest(hero.findEnemies())
    if enemy:
        soldiers = hero.findFriends()
        soldierIndex = 0
        soldier = soldiers[soldierIndex]
        # Loop over all your soldiers and order them to attack.
        if soldierIndex < len(soldiers):
            # Use the 'attack' command to make your soldiers attack.
            #hero.command(soldier, "attack", enemy)
            hero.command(soldier, "attack", enemy)
            soldierIndex += 1

```

I know I’m doing something wrong, but I can’t figure out what. My hero moves around fine, grabs coins, and he summons soldiers without a problem. The problem is that the soldiers won’t attack and I can’t figure out what I’m doing wrong.

---

<div class="post-metadata">

**Author:** ![AnSeDra](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/ansedra/32/13123_2.png) [@AnSeDra](https://discourse.codecombat.com/u/AnSeDra)\
**Post date:** [May 6, 2020, 5:33pm UTC](https://discourse.codecombat.com/t/mountain-mercenaries-python/21439/2 "2020-05-06T17:33:40Z")

</div>

On line 17, you should put _while_ instead of _if_.

---

<div class="post-metadata">

**Author:** ![smijas](https://avatars.discourse-cdn.com/v4/letter/s/958977/32.png) [@smijas](https://discourse.codecombat.com/u/smijas)\
**Post date:** [May 6, 2020, 5:36pm UTC](https://discourse.codecombat.com/t/mountain-mercenaries-python/21439/3 "2020-05-06T17:36:21Z")

</div>

Thanks.  
That didn’t actually solve the issue (I’ve restarted the code a few times, so I messed up on that), BUT it did solve it when I also moved the soldier = soldiers[soldierIndex] into the loop. So thank you

---

<div class="post-metadata">

**Author:** ![AnSeDra](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/ansedra/32/13123_2.png) [@AnSeDra](https://discourse.codecombat.com/u/AnSeDra)\
**Post date:** [May 6, 2020, 5:37pm UTC](https://discourse.codecombat.com/t/mountain-mercenaries-python/21439/4 "2020-05-06T17:37:08Z")

</div>

Glad I could help! 😉
