I can't summon soldiers on mountain mercenaries

This is my code. I don’t know why it isn’t working.

while True:
    coin = hero.findItems()
    # Move to the nearest coin.
    # Use move instead of moveXY so you can command constantly.
    while coin:
        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
        while True:
            
            # Loop over all your soldiers and order them to attack.
            soldier = soldiers[soldierIndex]
            # Use the 'attack' command to make your soldiers attack
            hero.command(soldier, "attack", enemy)
            #hero.command(soldier, "attack", enemy)

this is the part I am having trouble with

I fixed part of it, but after he summons soldiers he stops collecting coins. This is my new code:

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

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

make the if enemy loop inside the else:

I tried that it still won’t work.

delete the coin=hero.findItems() line