# \[SOLVED\] Hunter and Prey Help

**URL:** <https://discourse.codecombat.com/t/solved-hunter-and-prey-help/16047>\
**Category:** Level Help\
**Created:** [September 27, 2018, 1:55pm UTC](https://discourse.codecombat.com/t/solved-hunter-and-prey-help/16047 "2018-09-27T13:55:04Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sharknado](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/sharknado/32/9124_2.png) [@Sharknado](https://discourse.codecombat.com/u/Sharknado)\
**Post date:** [September 27, 2018, 1:55pm UTC](https://discourse.codecombat.com/t/solved-hunter-and-prey-help/16047/1 "2018-09-27T13:55:04Z")

</div>

here is my code I need help.

```python
# Ogres are trying to take out your reindeer!
# Keep your archers back while summoning soldiers to attack.

def pickUpCoin():
    # Collect coins.
    coin = hero.findNearestItem
    if coin:
        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 = friend.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 = friend.findNearestEnemy()
    if enemy > 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:** ![Sharknado](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/sharknado/32/9124_2.png) [@Sharknado](https://discourse.codecombat.com/u/Sharknado)\
**Post date:** [September 27, 2018, 2:42pm UTC](https://discourse.codecombat.com/t/solved-hunter-and-prey-help/16047/2 "2018-09-27T14:42:46Z")

</div>

someone help please?

---

<div class="post-metadata">

**Author:** ![Gabriel\_Dupuis](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/gabriel_dupuis/32/7450_2.png) [@Gabriel\_Dupuis](https://discourse.codecombat.com/u/Gabriel_Dupuis)\
**Post date:** [September 27, 2018, 6:47pm UTC](https://discourse.codecombat.com/t/solved-hunter-and-prey-help/16047/3 "2018-09-27T18:47:56Z")

</div>

Line 8 TypeError: Cannot read property ‘x’ of undefined

What is on line 8?  
hero.moveXY(coin.pos.x, coin.pos.y)

property x of undefined. So coin is undefined.  
you need to define what is coin

coin = hero.findNearestItem?

---

<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:43pm UTC](https://discourse.codecombat.com/t/solved-hunter-and-prey-help/16047/4 "2020-03-23T20:43:51Z")

</div>

you need () after item = hero.findNearestItem

---

<div class="post-metadata">

**Author:** ![098765432123](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/098765432123/32/25850_2.png) [@098765432123](https://discourse.codecombat.com/u/098765432123)\
**Post date:** [July 14, 2020, 2:16pm UTC](https://discourse.codecombat.com/t/solved-hunter-and-prey-help/16047/5 "2020-07-14T14:16:06Z")

</div>

i need help on this lvl please 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.
    if coin:
        coin = hero.findItems()
        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", target)
    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.findNearest(archer.findenemies)
    if enemy and archer.distanceto(enemy) < 25:
        hero.command(archer, "attack", enemy)
    else:
        hero.command(archer, "move", archer.pos)
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(friends)
            pass

```

and welcome @InnerdWhap to the forum! my issue is that on the bottom line it says command archer is not defined.

---

<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:** [July 14, 2020, 2:19pm UTC](https://discourse.codecombat.com/t/solved-hunter-and-prey-help/16047/6 "2020-07-14T14:19:53Z")

</div>

Watch out for capitalization…it’ll byte you every time 😜

---

<div class="post-metadata">

**Author:** ![098765432123](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/098765432123/32/25850_2.png) [@098765432123](https://discourse.codecombat.com/u/098765432123)\
**Post date:** [July 14, 2020, 2:21pm UTC](https://discourse.codecombat.com/t/solved-hunter-and-prey-help/16047/7 "2020-07-14T14:21:21Z")

</div>

now on line 31 its saying archer has no method findNearest

---

<div class="post-metadata">

**Author:** ![098765432123](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/098765432123/32/25850_2.png) [@098765432123](https://discourse.codecombat.com/u/098765432123)\
**Post date:** [July 14, 2020, 2:21pm UTC](https://discourse.codecombat.com/t/solved-hunter-and-prey-help/16047/8 "2020-07-14T14:21:47Z")

</div>

this line

```python
    enemy = archer.findNearest(archer.findenemies)

```

---

<div class="post-metadata">

**Author:** ![AnSeDra](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/ansedra/32/13123_2.png) [@AnSeDra](https://discourse.codecombat.com/u/AnSeDra)\
**Post date:** [July 14, 2020, 2:23pm UTC](https://discourse.codecombat.com/t/solved-hunter-and-prey-help/16047/9 "2020-07-14T14:23:19Z")

</div>

> [@098765432123](#):
>
> ` enemy = archer.findNearest(archer.findenemies)`

You can use archer.findNearestEnemy() there.

Andrei

---

<div class="post-metadata">

**Author:** ![098765432123](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/098765432123/32/25850_2.png) [@098765432123](https://discourse.codecombat.com/u/098765432123)\
**Post date:** [July 14, 2020, 2:24pm UTC](https://discourse.codecombat.com/t/solved-hunter-and-prey-help/16047/10 "2020-07-14T14:24:28Z")

</div>

its saying the same thing but with findNearestEnemy

---

<div class="post-metadata">

**Author:** ![Anna](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/anna/32/8378_2.png) [@Anna](https://discourse.codecombat.com/u/Anna)\
**Post date:** [July 14, 2020, 2:25pm UTC](https://discourse.codecombat.com/t/solved-hunter-and-prey-help/16047/11 "2020-07-14T14:25:29Z")

</div>

> [@AnSeDra](#):
>
> > [@098765432123](#):
> >
> > `enemy = archer.findNearest(archer.findenemies)`

You forgot a parenthesis (or is it pair of parenthesis 🤔) at the end of **archer.findEnemies()**

---

<div class="post-metadata">

**Author:** ![AnSeDra](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/ansedra/32/13123_2.png) [@AnSeDra](https://discourse.codecombat.com/u/AnSeDra)\
**Post date:** [July 14, 2020, 2:25pm UTC](https://discourse.codecombat.com/t/solved-hunter-and-prey-help/16047/12 "2020-07-14T14:25:41Z")

</div>

Don’t forget those ().

Andrei

---

<div class="post-metadata">

**Author:** ![098765432123](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/098765432123/32/25850_2.png) [@098765432123](https://discourse.codecombat.com/u/098765432123)\
**Post date:** [July 14, 2020, 2:26pm UTC](https://discourse.codecombat.com/t/solved-hunter-and-prey-help/16047/13 "2020-07-14T14:26:51Z")

</div>

same issue here is my current code

```python
# Ogres are trying to take out your reindeer!
# Keep your archers back while summoning soldiers to attack.

def pickUpCoin():
    # Collect coins.
    if coin:
        coin = hero.findItems()
        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", target)
    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(archer.findEnemies())
    if enemy and archer.distanceto(enemy) < 25:
        hero.command(archer, "attack", enemy)
    else:
        hero.command(archer, "move", archer.pos)
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(friends)
            pass

```

---

<div class="post-metadata">

**Author:** ![098765432123](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/098765432123/32/25850_2.png) [@098765432123](https://discourse.codecombat.com/u/098765432123)\
**Post date:** [July 14, 2020, 2:29pm UTC](https://discourse.codecombat.com/t/solved-hunter-and-prey-help/16047/14 "2020-07-14T14:29:08Z")

</div>

need a screen shot? (20char)

---

<div class="post-metadata">

**Author:** ![Anna](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/anna/32/8378_2.png) [@Anna](https://discourse.codecombat.com/u/Anna)\
**Post date:** [July 14, 2020, 2:30pm UTC](https://discourse.codecombat.com/t/solved-hunter-and-prey-help/16047/15 "2020-07-14T14:30:07Z")

</div>

> [@098765432123](#):
>
> ```python
> enemy = soldier.findNearestEnemy
> 
> ```

You need a () at the end of this. Also, try defining soldier as `hero.findNearest(hero.findFriends())` You don’t have it properly defined as a commandable unit, so maybe this will help.

---

<div class="post-metadata">

**Author:** ![AnSeDra](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/ansedra/32/13123_2.png) [@AnSeDra](https://discourse.codecombat.com/u/AnSeDra)\
**Post date:** [July 14, 2020, 2:30pm UTC](https://discourse.codecombat.com/t/solved-hunter-and-prey-help/16047/16 "2020-07-14T14:30:46Z")

</div>

> [@098765432123](#):
>
> `archer.findEnemies())`

Delete this because you do not need to have anything in between the() from findNearestEnemy.

Andrei

---

<div class="post-metadata">

**Author:** ![098765432123](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/098765432123/32/25850_2.png) [@098765432123](https://discourse.codecombat.com/u/098765432123)\
**Post date:** [July 14, 2020, 2:31pm UTC](https://discourse.codecombat.com/t/solved-hunter-and-prey-help/16047/17 "2020-07-14T14:31:58Z")

</div>

it still has the same issue

---

<div class="post-metadata">

**Author:** ![Anna](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/anna/32/8378_2.png) [@Anna](https://discourse.codecombat.com/u/Anna)\
**Post date:** [July 14, 2020, 2:32pm UTC](https://discourse.codecombat.com/t/solved-hunter-and-prey-help/16047/18 "2020-07-14T14:32:06Z")

</div>

Also, You are telling your soldiers to attack target, which is an undefined variable. Try using enemy instead in this line:

> [@098765432123](#):
>
> ```python
> hero.command(soldier, "attack", target)
> 
> ```

---

<div class="post-metadata">

**Author:** ![098765432123](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/098765432123/32/25850_2.png) [@098765432123](https://discourse.codecombat.com/u/098765432123)\
**Post date:** [July 14, 2020, 2:32pm UTC](https://discourse.codecombat.com/t/solved-hunter-and-prey-help/16047/19 "2020-07-14T14:32:57Z")

</div>

![Screenshot (12)](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/2X/5/5bcf66b1273b9c0d1d399781062b8276c691e560.png)

---

<div class="post-metadata">

**Author:** ![098765432123](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/098765432123/32/25850_2.png) [@098765432123](https://discourse.codecombat.com/u/098765432123)\
**Post date:** [July 14, 2020, 2:33pm UTC](https://discourse.codecombat.com/t/solved-hunter-and-prey-help/16047/20 "2020-07-14T14:33:44Z")

</div>

and i forgot to mention that my hero dosent move

[Next page](https://discourse.codecombat.com/t/solved-hunter-and-prey-help/16047.md?page=2)
