Toil and Trouble Level Help

Toil and Trouble

I’ve been stuck on the level Toil and Trouble for about a week and I need help so really could you please help me I don’t want to be stuck here for a year so please help. Here’s my code.

# Ogre Witches have some unpleasant surprises ready for you.

# Define a chooseTarget function which takes a friend argument
# Returns the a target to attack, depending on the type of friend.
# Soldiers should attack the witches, archers should attack nearest enemy.
def chooseTarget(friend):
    if friend.type == 'archer':
        target = friend.findNearestEnemy()
        return target
    elif friend.type == 'soldier':
        target = friend.findNearest(friend.findByType('witch'))
        return target

while True:
    if hero.gold >= hero.costOf("soldier"):
        hero.summon("soldier")
    friends = hero.findFriends()
    for friend in friends:
        # Use your chooseTarget function to decide what to attack.
        if friend.type == 'archer' or friend.type == 'soldier':
            if chooseTarget(friend):
                hero.command(friend, "attack", chooseTarget(friend))