# Hunters and prey python help \[SOLVED\]

**URL:** <https://discourse.codecombat.com/t/hunters-and-prey-python-help-solved/16864>\
**Category:** Solved\
**Created:** [December 22, 2018, 8:23pm UTC](https://discourse.codecombat.com/t/hunters-and-prey-python-help-solved/16864 "2018-12-22T20:23:00Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sebaston260](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/sebaston260/32/9460_2.png) [@Sebaston260](https://discourse.codecombat.com/u/Sebaston260)\
**Post date:** [December 22, 2018, 8:23pm UTC](https://discourse.codecombat.com/t/hunters-and-prey-python-help-solved/16864/1 "2018-12-22T20:23:00Z")

</div>

Why does the line 32 don’t work ?

```python
# 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

```

---

<div class="post-metadata">

**Author:** ![xython](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/xython/32/7269_2.png) [@xython](https://discourse.codecombat.com/u/xython)\
**Post date:** [December 22, 2018, 9:04pm UTC](https://discourse.codecombat.com/t/hunters-and-prey-python-help-solved/16864/2 "2018-12-22T21:04:50Z")

</div>

Your first and second error lines are:

```python
    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 

```

---

<div class="post-metadata">

**Author:** ![Sebaston260](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/sebaston260/32/9460_2.png) [@Sebaston260](https://discourse.codecombat.com/u/Sebaston260)\
**Post date:** [December 22, 2018, 9:22pm UTC](https://discourse.codecombat.com/t/hunters-and-prey-python-help-solved/16864/3 "2018-12-22T21:22:50Z")

</div>

okay and what can I do to fix it ?

---

<div class="post-metadata">

**Author:** ![Deadpool198](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/deadpool198/32/16753_2.png) [@Deadpool198](https://discourse.codecombat.com/u/Deadpool198)\
**Post date:** [December 23, 2018, 8:19am UTC](https://discourse.codecombat.com/t/hunters-and-prey-python-help-solved/16864/4 "2018-12-23T08:19:21Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Warhawk721](https://avatars.discourse-cdn.com/v4/letter/w/ecc23a/32.png) [@Warhawk721](https://discourse.codecombat.com/u/Warhawk721)\
**Post date:** [January 22, 2019, 3:31am UTC](https://discourse.codecombat.com/t/hunters-and-prey-python-help-solved/16864/5 "2019-01-22T03:31:02Z")

</div>

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:

```python
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

```

---

<div class="post-metadata">

**Author:** ![Warhawk721](https://avatars.discourse-cdn.com/v4/letter/w/ecc23a/32.png) [@Warhawk721](https://discourse.codecombat.com/u/Warhawk721)\
**Post date:** [January 22, 2019, 3:37am UTC](https://discourse.codecombat.com/t/hunters-and-prey-python-help-solved/16864/6 "2019-01-22T03:37:15Z")

</div>

Never mind I solved it.

---

<div class="post-metadata">

**Author:** ![zython](https://avatars.discourse-cdn.com/v4/letter/z/65b543/32.png) [@zython](https://discourse.codecombat.com/u/zython)\
**Post date:** [July 15, 2020, 3:05am UTC](https://discourse.codecombat.com/t/hunters-and-prey-python-help-solved/16864/7 "2020-07-15T03:05:50Z")

</div>

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

```

---

<div class="post-metadata">

**Author:** ![Deadpool198](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/deadpool198/32/16753_2.png) [@Deadpool198](https://discourse.codecombat.com/u/Deadpool198)\
**Post date:** [July 15, 2020, 6:41am UTC](https://discourse.codecombat.com/t/hunters-and-prey-python-help-solved/16864/8 "2020-07-15T06:41:51Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![zython](https://avatars.discourse-cdn.com/v4/letter/z/65b543/32.png) [@zython](https://discourse.codecombat.com/u/zython)\
**Post date:** [July 16, 2020, 9:04am UTC](https://discourse.codecombat.com/t/hunters-and-prey-python-help-solved/16864/9 "2020-07-16T09:04:14Z")

</div>

I was typing it for @Warhawk721

---

<div class="post-metadata">

**Author:** ![Deadpool198](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/deadpool198/32/16753_2.png) [@Deadpool198](https://discourse.codecombat.com/u/Deadpool198)\
**Post date:** [July 16, 2020, 9:08am UTC](https://discourse.codecombat.com/t/hunters-and-prey-python-help-solved/16864/10 "2020-07-16T09:08:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![zython](https://avatars.discourse-cdn.com/v4/letter/z/65b543/32.png) [@zython](https://discourse.codecombat.com/u/zython)\
**Post date:** [July 16, 2020, 9:09am UTC](https://discourse.codecombat.com/t/hunters-and-prey-python-help-solved/16864/11 "2020-07-16T09:09:23Z")

</div>

Never mind, I solved it
