Mountain Mercenaries(Command Problem)

I tried playing the level with the code below but it keeps giving me the error message “command’s argument minion should have type unit, but got null. Hero Placeholder needs something to command.”
Here’s my code:

# 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.
    item = hero.findNearestItem()
    hero.moveXY(item.pos.x, item.pos.y)
    # 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.
        hero.command(soldier,"attack",enemy)
        soldierIndex+=1
            # Use the 'attack' command to make your soldiers attack.
            #hero.command(soldier, "attack", enemy)

There’s a couple of things that need fixing. First, is there always an item? The way you’ve written this, if there is ever a time when there are no items, the code will fail. Second, you’re missing a very important line with regard to looping over all your soldiers. You need a while loop after the line, soldierIndex = 0. Review some of the previous levels that use similar code to iterate over an array of objects.