Hunters and prey mistake (by me)

# Ogres are trying to take out your reindeer!
# Keep your archers back while summoning soldiers to attack.

def pickUpCoin():
    # Collect coins.
    item = hero.findNearestItem()
    if item:
        hero.moveXY(item.pos.x, item.pos.y)

def summonTroops():
    # Summon soldiers if you have the gold.
    if > hero.costOf("soldier"):
# This function has an argument named soldier.
# Arguments are like variables.
# The value of an argument is determined when the function is called.
def commandSoldier(soldier):
    # Soldiers should attack enemies.
    soldiers = hero.findFriends()
    for soldier in soldiers:
        enemy = soldier.findNearestEnemy()
        if enemy:
            hero.command(soldier, "attack", enemy)

def commandArcher(archer):
    # Soldiers should attack enemies.
    archers = hero.findFriends()
    for archer in archers:
        enemy = archer.findNearestEnemy()
        if enemy and archer.distanceTo(enemy) < 25:
            hero.command(archer, "attack", enemy)
# Write a commandArcher function to tell your archers what to do!
# It should take one argument that will represent the archer passed to the function when it's called.
# Archers should only attack enemies who are closer than 25 meters, otherwise, stay still.

while True:
    friends = hero.findFriends()
    for friend in friends:
        if friend.type == "soldier":
            # This friend will be assigned to the variable soldier in commandSoldier
        elif friend.type == "archer":
            # Be sure to command your archers.

My archers just keep attacking and not abiding the distance; what have I done wrong?!


I’m not sure, but maybe an else in

might help

def commandSoldier(soldier):
                                   # you have an unique soldier coming from main
    # Soldiers should attack enemies.
    soldiers = hero.findFriends()  # not soldiers only, archers and reindeer 
    for soldier in soldiers:
    # code follows