Hunters and Prey (defined problem)

def pickUpCoin():
    # Collect coins.
    coin = hero.findNearestItem()
    if coin:
        hero.move(coin.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(soldier):
    # Soldiers should attack enemies.
    enemy = soldier.findNearestEnemy()
    if enemy:
        hero.command(soldier, "attack", enemy)
    pass

def commandArcher(archer):
    enemy = archer.findNearestEnemy()
    distance = archer.distanceTo(enemy)
    for archer in hero.findFriends():
        if enemy:
            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:
    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 

help, it say that the: archer is not defined

1 Like

Hi 11125!
Your problem’s within your while True: loop.
the parts with

commandSoldier(soldier)

and

commandArcher(archer)

you never define the variable soldier and never define the variable archer.
What might be confusing is that the function require an argument, where it’s called archer or soldier, however, think of it as setting this variable for the function.

For example:

def addOne(number):
   result = number + 1
   return result

if you’d have a function like this, you wouldn’t want to use it with addOne(number)
but instead, use it like addOne(1), which would return 2 (adding 1 to 1).
so what it’s called in the ( and ) when defining a function is simply a variable for the input

In your case, however, you do something with every element within an array with

for friend in friends:

friend in this case refers to the unit you would like to command, so there’s no need to define either soldier or archer, as you can use it as is like you have it in the if statements

if friend.type == "soldier":
   #... 
#and also
if friend.type == "archer":
   #...  

Sorry for misspellings, possible errors and possibly explaining more than necessary (I just like to try to make it easily understandable). If you find it hard to understand my explanation, I’d be glad to try to explain it differently (or others will take my place, if I’m unable to :sweat_smile:) , just ask away! :slight_smile:

3 Likes

thank you(*´∀`)~♥ I will get it done as soon as passable!

1 Like