# Hunters and Prey Python Help

**URL:** <https://discourse.codecombat.com/t/hunters-and-prey-python-help/20217>\
**Category:** Level Help\
**Created:** [February 8, 2020, 6:22pm UTC](https://discourse.codecombat.com/t/hunters-and-prey-python-help/20217 "2020-02-08T18:22:12Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Awesome000000000](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/awesome000000000/32/11386_2.png) [@Awesome000000000](https://discourse.codecombat.com/u/Awesome000000000)\
**Post date:** [February 8, 2020, 6:22pm UTC](https://discourse.codecombat.com/t/hunters-and-prey-python-help/20217/1 "2020-02-08T18:22:12Z")

</div>

Here is my code:

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

```

For some reason, the archers attack everyone just like the soldiers do. Can someone please help me figure out what is going wrong?

---

<div class="post-metadata">

**Author:** ![dedreous](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/dedreous/32/13849_2.png) [@dedreous](https://discourse.codecombat.com/u/dedreous)\
**Post date:** [February 8, 2020, 6:57pm UTC](https://discourse.codecombat.com/t/hunters-and-prey-python-help/20217/2 "2020-02-08T18:57:24Z")

</div>

Code looks good overall…but, what do your archers do if the distance is not \< 25?

---

<div class="post-metadata">

**Author:** ![Awesome000000000](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/awesome000000000/32/11386_2.png) [@Awesome000000000](https://discourse.codecombat.com/u/Awesome000000000)\
**Post date:** [February 8, 2020, 9:40pm UTC](https://discourse.codecombat.com/t/hunters-and-prey-python-help/20217/3 "2020-02-08T21:40:32Z")

</div>

It doesn’t matter how far anything is away they just try to kill it. Then they get too far away from the reindeer and the reindeer get killed.

---

<div class="post-metadata">

**Author:** ![Awesome000000000](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/awesome000000000/32/11386_2.png) [@Awesome000000000](https://discourse.codecombat.com/u/Awesome000000000)\
**Post date:** [February 8, 2020, 9:43pm UTC](https://discourse.codecombat.com/t/hunters-and-prey-python-help/20217/4 "2020-02-08T21:43:23Z")

</div>

![Screen Shot 2020-02-08 at 3.42.36 PM](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/2X/a/a8e78a6b851b57c704e2b733848a1bb0e3bee1e0.png)  
Here is my gear if that helps.

---

<div class="post-metadata">

**Author:** ![dedreous](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/dedreous/32/13849_2.png) [@dedreous](https://discourse.codecombat.com/u/dedreous)\
**Post date:** [February 8, 2020, 9:44pm UTC](https://discourse.codecombat.com/t/hunters-and-prey-python-help/20217/5 "2020-02-08T21:44:17Z")

</div>

How about having them stay in place if the distance is not \< 25? In other words, think about what ELSE they should do 😉

---

<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:** [February 8, 2020, 10:33pm UTC](https://discourse.codecombat.com/t/hunters-and-prey-python-help/20217/6 "2020-02-08T22:33:10Z")

</div>

```python
   def commandSoldier(): 
    for soldier in hero.findFriends():
    # is your soldier a "soldier" 

def commandArcher():
    for archer in hero.findFriends():
    # is your archer an "archer"

while True:
# code
            commandSoldier("soldier")
# you defined above function as commandSoldier() - no argument
            commandArcher("archer")
# you defined above function as commandArcher() - no argument

```

---

<div class="post-metadata">

**Author:** ![Awesome000000000](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/awesome000000000/32/11386_2.png) [@Awesome000000000](https://discourse.codecombat.com/u/Awesome000000000)\
**Post date:** [February 8, 2020, 11:28pm UTC](https://discourse.codecombat.com/t/hunters-and-prey-python-help/20217/7 "2020-02-08T23:28:06Z")

</div>

Here is my new definition of commandArcher:

```python
def commandArcher():
    for archer in hero.findFriends():
        enemy = archer.findNearestEnemy()
        if enemy and archer.distanceTo(enemy) < 25:
            hero.command(archer, "attack", enemy)
        else:
            hero.moveXY(23, 46)
        
    pass

```

But they don’t move back to the position I want them to.

---

<div class="post-metadata">

**Author:** ![Alexbrand](https://avatars.discourse-cdn.com/v4/letter/a/e9bcb4/32.png) [@Alexbrand](https://discourse.codecombat.com/u/Alexbrand)\
**Post date:** [February 8, 2020, 11:34pm UTC](https://discourse.codecombat.com/t/hunters-and-prey-python-help/20217/8 "2020-02-08T23:34:34Z")

</div>

Maybe it’ better to divide enemy existance and distance to it?

---

<div class="post-metadata">

**Author:** ![Awesome000000000](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/awesome000000000/32/11386_2.png) [@Awesome000000000](https://discourse.codecombat.com/u/Awesome000000000)\
**Post date:** [February 8, 2020, 11:39pm UTC](https://discourse.codecombat.com/t/hunters-and-prey-python-help/20217/9 "2020-02-08T23:39:32Z")

</div>

Oh no. I feel so dumb I said `hero.move` instead of `hero.command`

---

<div class="post-metadata">

**Author:** ![dedreous](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/dedreous/32/13849_2.png) [@dedreous](https://discourse.codecombat.com/u/dedreous)\
**Post date:** [February 8, 2020, 11:42pm UTC](https://discourse.codecombat.com/t/hunters-and-prey-python-help/20217/10 "2020-02-08T23:42:34Z")

</div>

But, your still need each archer stay in his own place, if there is nothing going on.

---

<div class="post-metadata">

**Author:** ![Awesome000000000](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/awesome000000000/32/11386_2.png) [@Awesome000000000](https://discourse.codecombat.com/u/Awesome000000000)\
**Post date:** [February 8, 2020, 11:42pm UTC](https://discourse.codecombat.com/t/hunters-and-prey-python-help/20217/11 "2020-02-08T23:42:39Z")

</div>

Wait what the heck it only worked when I made my hero not collect coins. I don’t think that’s how it is supposed to work, but ok.
