# Hunters and prey help

**URL:** <https://discourse.codecombat.com/t/hunters-and-prey-help/20619>\
**Category:** Level Help\
**Created:** [March 23, 2020, 8:50pm UTC](https://discourse.codecombat.com/t/hunters-and-prey-help/20619 "2020-03-23T20:50:26Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![InnerdWhap](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/innerdwhap/32/11643_2.png) [@InnerdWhap](https://discourse.codecombat.com/u/InnerdWhap)\
**Post date:** [March 23, 2020, 8:50pm UTC](https://discourse.codecombat.com/t/hunters-and-prey-help/20619/1 "2020-03-23T20:50:26Z")

</div>

Trying to beat “Hunters and Prey”, but my archers always move when there’s an enemy, even if the distance is move than 25. Help please! Here is my 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.moveXY(item.pos.x, item.pos.y)  
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(friend, “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 = hero.findNearestEnemy()  
if enemy:  
archerDistance = archer.distanceTo(enemy)  
if archerDistance \<= 25:  
hero.command(friend, “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:** ![ducky](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/ducky/32/9364_2.png) [@ducky](https://discourse.codecombat.com/u/ducky)\
**Post date:** [March 23, 2020, 9:37pm UTC](https://discourse.codecombat.com/t/hunters-and-prey-help/20619/2 "2020-03-23T21:37:33Z")

</div>

Please read the FAQ to format your code properly:

> [@FAQ - Check Before Posting](https://discourse.codecombat.com/t/faq-check-before-posting/1027):
>
> Check This Awesome FAQ Before Posting … you know it’s sweet stuff! Might save your life some day. [I need help with my code!](#code-help) Check each level’s help guide for common tips If a level is too hard, try a different hero or different items Common error messages and how to debug them Post your code with radiant, harmonious formatting Personal coding help from the CodeCombat staff [The game is not working!](#not-working) It crashes It doesn’t load It’s slow The graphics aren’t showing up [Can you please fix …](#bugs)

---

<div class="post-metadata">

**Author:** ![Noob\_Newbie](https://avatars.discourse-cdn.com/v4/letter/n/5daacb/32.png) [@Noob\_Newbie](https://discourse.codecombat.com/u/Noob_Newbie)\
**Post date:** [March 24, 2020, 9:28am UTC](https://discourse.codecombat.com/t/hunters-and-prey-help/20619/3 "2020-03-24T09:28:41Z")

</div>

> [@InnerdWhap](#):
>
> def commandArcher(archer):  
> enemy = hero.findNearestEnemy()  
> if enemy:  
> archerDistance = archer.distanceTo(enemy)  
> if archerDistance \<= 25:  
> hero.command(friend, “attack”, enemy)

Is there a problem here because you didn’t define friend in this function

---

<div class="post-metadata">

**Author:** ![Noob\_Newbie](https://avatars.discourse-cdn.com/v4/letter/n/5daacb/32.png) [@Noob\_Newbie](https://discourse.codecombat.com/u/Noob_Newbie)\
**Post date:** [March 24, 2020, 9:31am UTC](https://discourse.codecombat.com/t/hunters-and-prey-help/20619/4 "2020-03-24T09:31:05Z")

</div>

Will it be better if you make the archers move [‘x’:archer.pos.x, ‘y’:archer.pos.y] in “if enemy”, and only attack if enemy is within range

---

<div class="post-metadata">

**Author:** ![ducky](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/ducky/32/9364_2.png) [@ducky](https://discourse.codecombat.com/u/ducky)\
**Post date:** [March 24, 2020, 5:48pm UTC](https://discourse.codecombat.com/t/hunters-and-prey-help/20619/5 "2020-03-24T17:48:12Z")

</div>

All you need to do is to change “friend” into either “archer” or “soldier” in your functions, because the program doesn’t know what a “friend” is, because when you use args, it only knows what a “soldier” or an “archer” is because

> [@InnerdWhap](#):
>
> def commandArcher(archer):

you’re defining it like that.
