[SOLVED] Mountain Mercenaries Help

I am mostly sure I have a correct code based upon what I have found looking at the discourse. Having said this, something seems to happen when there aren’t any enemies: the soldiers stop doing things and don’t attack new enemies until a new soldier is summoned. This has caused my soldiers to become overwhelmed and I can’t pass the level. Is this a bug, or have I just done poorly designing my code to organize the soldiers?

# Gather coins to summon soldiers and have them attack the enemy.

while True:
    # Move to the nearest coin.
    # Use move instead of moveXY so you can command constantly.
    coin = hero.findNearestItem()
    if coin:
        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.
        while soldierIndex < len(soldiers):
            # Use the 'attack' command to make your soldiers attack.
            #hero.command(soldier, "attack", enemy)
            soldier = soldiers[soldierIndex]
            hero.command(soldier, "attack", enemy)
            soldierIndex += 1

Your code is weird, your hero search for enemy only if he has enough gold.

if hero.gold > hero.costOf("soldier"):
        hero.summon("soldier")
        enemy = hero.findNearest(hero.findEnemies())

Also you might get other type of errors because, soldiers might kill the enemy and then they will attack a null target.

Yes, Gabriel is correct. This is a structural issue. You define enemy inside a conditional statement. Tab it back and the code works fine.

Okay, I see what you mean. I’m sorry if I freaked anybody out. I will check my indenting even more carefully next time. Is there some way to put solved in the title?