[SOLVED]Help in Hunter's prey

You’ll want to define 2 different variables for these two.
Get rid of those quotes here:

and here:

Lydia

for here after the if satement write a


else:
        
        hero.command(archer, "move", archer.pos)

here is my new code and it still doesn’t work:

# 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, "move", archer.pos)
        else:
            hero.command(archer, "attack", enemy)
        
    pass

while True:
    enemy = soldier.findNearestEnemy
    enemy = archer.findNearestEnemy
    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

I found the problem put this in it’s own if statement.

So I should create a new if statement?

Yeah do it like this

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

so I create a new one at the bottom of my code correct?

So get rid of this

and replace it with this

            if enemyDistance < 25:
                
                if friend.type == "archer":
                    hero.command(friend, "attack", enemy)
                
            else:
                if enemyDistance > 25:
                    
                    hero.command(friend, "move", friend.pos)```
1 Like

it say’s enemy = soldier.findNearestEnemy is not defined for soldier

do

enemy = friend.findNearestEnemy()

It is okay if you hero does it instead of the soldier like this

enemy = hero.findNearestEnemy

here is what it say’s when I added the code:

here is what I use:

This should be if enemy and archar.distanceTo(enemy) < 25, then command archer to attack the enemy. Else stay still

Both need () at the end
Otherwise, looks good!
Lydia

1 Like

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 enemyDistance < 25:
                
            if friend.type == "archer":
                hero.command(friend, "attack", enemy)
                
            else:
                if enemyDistance > 25:
                    
                    hero.command(friend, "move", friend.pos)
        
    pass

while True:
    enemy = soldier.findNearestEnemy()
    enemy = archer.findNearestEnemy()
    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

I dont think you need this.
soldier and archer are also not defiened so I think you can delete this part

For these to places instead of archer or soldier put in `friend

`

Try deleting these as Falcons118 have said.
Lydia

Do that and what I said then you should be good.

no because soldier and archer are not defined so @Ryan_Wong will have to remove that