Hunters and prey python help [SOLVED]

Why does the line 32 don’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 > 20:
        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):
    # Soldiers should attack enemies.
    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(archer):
    enemy = archer.findNearestEnemy
    distance = archer.distanceTo(enemy)
    if enemy and distance < 25:
        hero.command(archer, "attack", enemy)
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.
            commandArcher(friend)
            pass

Your first and second error lines are:

    enemy = archer.findNearestEnemy
    # your are calling a function and have forgotten something
    distance = archer.distanceTo(enemy)
    # even if the above line is correct you will measure a distance
    # to an  object that probably doesn't exist initially 

okay and what can I do to fix it ?

Read what xython has said carefully.
You’ve seen these things in earlier levels.
:lion:

My archers just move to the nearest enemy. I read the above messages and I think I have the 2nd problem, but I don’t know how to fix it. Here is my code:

def pickUpCoin():
    # Collect coins.
    coin = hero.findNearestItem()
    hero.moveXY(coin.pos.x, coin.pos.y)
    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(soldier):
    # Soldiers should attack enemies.
    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(archer):
    enemy = archer.findNearestEnemy()
    if enemy and archer.distanceTo(enemy) < 25:
        hero.command(archer, "attack", enemy)
    else:
        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(friend)
        elif friend.type == "archer":
            # Be sure to command your archers.
            commandArcher(friend)
            pass

Never mind I solved it.

#you can't write soldier or archer in the functions 
#or it will think that it was a function
#do this:
def commandArcher():
#and
def commandSoldier():
#write ' friends = findFriends()
           #for friend in friends:
           #if friend:
              #if friend.type = ('Archer'#or Soldier)
                   #hero.command(NaN, NaN, NaN) 
#add a distance variable to calculate the distance, use if friend.distanceTo(~~~) in the if stmt.

What is this? Have you written any code yet? Do you need help?
Please explain your post.
Danny

2 Likes

I was typing it for @Warhawk721

Ok, thanks for trying to help, but please don’t revive old topics. It won’t benefit the original poster (he/she’s not active anymore and has probably solved the problem).
Danny

1 Like

Never mind, I solved it