[SOLVED]Help in Hunter's prey

I getting stuck since the enemy appear on the sides
here is my code:

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

def pickUpCoin():
    # Collect coins.
    items = hero.findItems()
    nearest = hero.findNearest(items)
    if nearest:
         hero.move(nearest.pos)

def summonTroops():
    # Summon soldiers if you have the gold.
    if hero.gold > hero.costOf("soldier"):
        hero.summon("soldier")
    pass
    
# 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):
    enemy = hero.findNearestEnemy()
    # Soldiers should attack enemies.
    hero.command(soldier, "attack", enemy)

In this one, you need to define friends and use a for loop to command each soldier to attack the enemy.

After this, you need to write a commandArcher function. This function is basically the same as the commandSoldier function but archers should only attack enemies who are closer than 25 meters (in game), otherwise, stay still.
This is comments from the game:

# 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.

Then you just need to use the functions in a while True loop. But you also have to use a for-loop for commanding your friends.
Lydia

huh I don’t get what you’re saying
@JoPro_8000 pls come
@Eric_Tang pls come too

so do something like this

enemyDistance = hero.distanceTo(enemy)
        for friend in friends:
            if enemyDistance < 25:
                
                if friend.type == "archer":
                    hero.command(friend, "attack", enemy)
                
            else:
                if enemyDistance > 25:
                    
                    hero.command(friend, "move", friend.pos)

but where tho (2o chars)

this is what’s not making any sense to me at all

Show your new updated code now

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

def pickUpCoin():
    # Collect coins.
    items = hero.findItems()
    nearest = hero.findNearest(items)
    if nearest:
         hero.move(nearest.pos)

def summonTroops():
    # Summon soldiers if you have the gold.
    if hero.gold > hero.costOf("soldier"):
        hero.summon("soldier")
    pass
    
# 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):
    enemy = hero.findNearestEnemy()
    # Soldiers should attack enemies.
    hero.command(soldier, "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:
    pickUpCoin()
    summonTroops()
    friends = hero.findFriends()
    for friend in friends:
        if friend.type == "soldier":
            # This friend will be assigned to the variable soldier in commandSoldier
            commandSoldier(friend)
        elif friend.type == "archer":
            # Be sure to command your archers.
            enemyDistance = hero.distanceTo(enemy)
        for friend in friends:
            if enemyDistance < 25:
                
                if friend.type == "archer":
                    hero.command(friend, "attack", enemy)
                
            else:
                if enemyDistance > 25:
                    
                    hero.command(friend, "move", friend.pos)
            pass

that’s what I did ()()()())()(()()()()()(

almost got it just put all that I told you in a function
like this

def commandArcher():
    enemy = hero.findNearestEnemy()
    friends = hero.findByType("archer", hero.findFriends())
    if enemy:
        enemyDistance = hero.distanceTo(enemy)
        for friend in friends:
            if enemyDistance < 25:
                
                if friend.type == "archer":
                    hero.command(friend, "attack", enemy)
                
            else:
                if enemyDistance > 25:
                    
                    hero.command(friend, "move", friend.pos)
    

Then in the loop put this in this in the place of the function

        if friend.type == "archer":
            commandArcher(archer)

Do I put that code at the end?

yeah post your new code now(if you have any)

here is my new code:

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

def pickUpCoin():
    # Collect coins.
    items = hero.findItems()
    nearest = hero.findNearest(items)
    if nearest:
         hero.move(nearest.pos)

def summonTroops():
    # Summon soldiers if you have the gold.
    if hero.gold > hero.costOf("soldier"):
        hero.summon("soldier")
    pass
    
# 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):
    enemy = hero.findNearestEnemy()
    # Soldiers should attack enemies.
    hero.command(soldier, "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:
    pickUpCoin()
    summonTroops()
    friends = hero.findFriends()
    for friend in friends:
        if friend.type == "soldier":
            # This friend will be assigned to the variable soldier in commandSoldier
            commandSoldier(friend)
        elif friend.type == "archer":
            # Be sure to command your archers.
            def commandArcher():
                enemy = hero.findNearestEnemy()
        friends = hero.findByType("archer", hero.findFriends())
    if enemy:
        enemy = hero.findNearestEnemy()
        enemyDistance = hero.distanceTo(enemy)
        for friend in friends:
            if enemyDistance < 25:
                
                if friend.type == "archer":
                    hero.command(friend, "attack", enemy)
                
            else:
                if enemyDistance > 25:
                    
                    hero.command(friend, "move", friend.pos)
            if friend.type == "archer":
                commandArcher(archer)

it still doesn’t work

Almost got it just put this out of the loop like a function like commandSoldiers(). Also when you get that out of the loop put the right indentation.

like this:

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

def pickUpCoin():
    # Collect coins.
    items = hero.findItems()
    nearest = hero.findNearest(items)
    if nearest:
         hero.move(nearest.pos)

def summonTroops():
    # Summon soldiers if you have the gold.
    if hero.gold > hero.costOf("soldier"):
        hero.summon("soldier")
    pass
    
# 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):
    enemy = hero.findNearestEnemy()
    # Soldiers should attack enemies.
    hero.command(soldier, "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:
    pickUpCoin()
    summonTroops()
    friends = hero.findFriends()
for friend in friends:
    if friend.type == "soldier":
        commandSoldier(friend)
    elif friend.type == "archer":
        # Be sure to command your archers.
        def commandArcher():
            enemy = hero.findNearestEnemy()
        friends = hero.findByType("archer", hero.findFriends())
    if enemy:
        enemy = hero.findNearestEnemy()
        enemyDistance = hero.distanceTo(enemy)
        for friend in friends:
            if enemyDistance < 25:
                
                if friend.type == "archer":
                    hero.command(friend, "attack", enemy)
                
            else:
                if enemyDistance > 25:
                    
                    hero.command(friend, "move", friend.pos)
            if friend.type == "archer":
                commandArcher(archer)

here is my new code:

# 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.move(item.pos)
    pass

def summonTroops():
    # Summon soldiers if you have the gold.
    if hero.gold > hero.costOf("soldier"):
        hero.summon("soldier")
    pass
    
# 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():
    for soldier in hero.findFriends():
        enemy = soldier.findNearestEnemy()
        if enemy:
            hero.command(soldier, "attack", enemy)
    pass

# 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.
def commandArcher():
    for archer in hero.findFriends():
        enemy = archer.findNearestEnemy()
        if enemy and archer.distanceTo(enemy) < 25:
            hero.command(archer, "attack", enemy)
        
    pass

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

Get rid of these ""
Here

And here

giving you

commandArcher(archer)

And

commandSoldier(soldier)

So in commandSoldier function, you first need to define friends as friends = hero.findFriends(), and then use a for-loop to command each soldier to attack the enemy (perferably the nearest to them)

Then you need to write commandArcher function to command the archers to attack enemies that is closer than 25 meters, if the enemy is more than 25 meters away, then stay still.

Then use a while True loop to use the functions.
Lydia