# Hunting Party coding help

**URL:** <https://discourse.codecombat.com/t/hunting-party-coding-help/27107>\
**Category:** Level Help\
**Created:** [January 29, 2021, 12:42am UTC](https://discourse.codecombat.com/t/hunting-party-coding-help/27107 "2021-01-29T00:42:47Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Wesley\_Leung](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/wesley_leung/32/13935_2.png) [@Wesley\_Leung](https://discourse.codecombat.com/u/Wesley_Leung)\
**Post date:** [January 29, 2021, 12:42am UTC](https://discourse.codecombat.com/t/hunting-party-coding-help/27107/1 "2021-01-29T00:42:47Z")

</div>

I am stuck on Hunting Party, mountain level. This is my code:

```python
def spawn():
    hero.move(hero.pos)
    hero.move(hero.pos)
    hero.move(hero.pos)
    hero.move(hero.pos)
    hero.move(hero.pos)
    hero.moveXY(44, 67)
    while enemy:
        if hero.isReady("bash"):
            hero.bash(enemy)
        elif friend.health < 10:
            hero.consecrate()
        else:
            hero.attack(enemy)
        
pet.on("spawn", spawn)
while True:
    friends = hero.findFriends()
    # Use for-loop and for each friend:
    for i in range(len(friends)):
        # If they see an enemy then command to attack.
        friend = hero.findNearest(hero.findFriends())
        enemy = friend.findNearestEnemy()
        if enemy:
            hero.command(friend, "attack", enemy)
            # Command to move east by small steps.
        else:
            y = friend.pos.y
            x = friend.pos.x + 1000
            hero.command(friend, "move", {"x": x, "y": y})

```

---

<div class="post-metadata">

**Author:** ![Grzmot](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/grzmot/32/13514_2.png) [@Grzmot](https://discourse.codecombat.com/u/Grzmot)\
**Post date:** [January 29, 2021, 12:49am UTC](https://discourse.codecombat.com/t/hunting-party-coding-help/27107/2 "2021-01-29T00:49:25Z")

</div>

Wait, are we talking about the same hunting party? Does your level look like this:  
 ![image](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/3X/6/5/65bfd1ebbce33ec03bb08c5562c5b38b1c861d9e.png)  
-Grzmot

---

<div class="post-metadata">

**Author:** ![Wesley\_Leung](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/wesley_leung/32/13935_2.png) [@Wesley\_Leung](https://discourse.codecombat.com/u/Wesley_Leung)\
**Post date:** [January 29, 2021, 12:50am UTC](https://discourse.codecombat.com/t/hunting-party-coding-help/27107/3 "2021-01-29T00:50:09Z")

</div>

yes, it is this level.

---

<div class="post-metadata">

**Author:** ![Wesley\_Leung](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/wesley_leung/32/13935_2.png) [@Wesley\_Leung](https://discourse.codecombat.com/u/Wesley_Leung)\
**Post date:** [January 29, 2021, 12:53am UTC](https://discourse.codecombat.com/t/hunting-party-coding-help/27107/4 "2021-01-29T00:53:19Z")

</div>

also, my code had to be reset because of infinite loop…

---

<div class="post-metadata">

**Author:** ![Grzmot](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/grzmot/32/13514_2.png) [@Grzmot](https://discourse.codecombat.com/u/Grzmot)\
**Post date:** [January 29, 2021, 12:53am UTC](https://discourse.codecombat.com/t/hunting-party-coding-help/27107/5 "2021-01-29T00:53:19Z")

</div>

Okay, It looks like you have a very different strategy than what the level wants you to do. Try using a while loop and command your friends to attack an enemy if there is one. If not, try moving them forward. It should look something like this:

```python
while True:
    friends = hero.findFriends()
    for friend in friends:
        (Tell them to find an enemy)
        if enemy:
            (command them to attack the enemy)
        else:
            (Command them to move forward)

```

Hope this helps!  
-Grzmot

---

<div class="post-metadata">

**Author:** ![Wesley\_Leung](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/wesley_leung/32/13935_2.png) [@Wesley\_Leung](https://discourse.codecombat.com/u/Wesley_Leung)\
**Post date:** [January 29, 2021, 12:58am UTC](https://discourse.codecombat.com/t/hunting-party-coding-help/27107/6 "2021-01-29T00:58:06Z")

</div>

uhh, there is a weird error on my code. it says that “enemy” is not a unit…

---

<div class="post-metadata">

**Author:** ![Grzmot](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/grzmot/32/13514_2.png) [@Grzmot](https://discourse.codecombat.com/u/Grzmot)\
**Post date:** [January 29, 2021, 12:59am UTC](https://discourse.codecombat.com/t/hunting-party-coding-help/27107/7 "2021-01-29T00:59:28Z")

</div>

Is it the same code that you posted before or is it edited now?  
-Grzmot

---

<div class="post-metadata">

**Author:** ![Wesley\_Leung](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/wesley_leung/32/13935_2.png) [@Wesley\_Leung](https://discourse.codecombat.com/u/Wesley_Leung)\
**Post date:** [January 29, 2021, 1:00am UTC](https://discourse.codecombat.com/t/hunting-party-coding-help/27107/8 "2021-01-29T01:00:54Z")

</div>

edited… and the original code in

```python
hero.command(friend, "attack", enemy)

```

---

<div class="post-metadata">

**Author:** ![Grzmot](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/grzmot/32/13514_2.png) [@Grzmot](https://discourse.codecombat.com/u/Grzmot)\
**Post date:** [January 29, 2021, 1:03am UTC](https://discourse.codecombat.com/t/hunting-party-coding-help/27107/9 "2021-01-29T01:03:24Z")

</div>

I think that the issue might be coming from this:

> [@Wesley\_Leung](#):
>
> ```python
> for i in range(len(friends)):
> # If they see an enemy then command to attack.
> friend = hero.findNearest(hero.findFriends())
> 
> ```

If you want to use this kind of loop, try changing the second part to: `friend = friends[i]` so it will cycle through all of your friends. Hope this helps!  
-Grzmot

---

<div class="post-metadata">

**Author:** ![Wesley\_Leung](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/wesley_leung/32/13935_2.png) [@Wesley\_Leung](https://discourse.codecombat.com/u/Wesley_Leung)\
**Post date:** [January 29, 2021, 1:04am UTC](https://discourse.codecombat.com/t/hunting-party-coding-help/27107/10 "2021-01-29T01:04:07Z")

</div>

before, it worked. now it doesn’t…

---

<div class="post-metadata">

**Author:** ![Grzmot](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/grzmot/32/13514_2.png) [@Grzmot](https://discourse.codecombat.com/u/Grzmot)\
**Post date:** [January 29, 2021, 1:05am UTC](https://discourse.codecombat.com/t/hunting-party-coding-help/27107/11 "2021-01-29T01:05:26Z")

</div>

Could you show me your full updated code now?  
-Grzmot

---

<div class="post-metadata">

**Author:** ![Wesley\_Leung](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/wesley_leung/32/13935_2.png) [@Wesley\_Leung](https://discourse.codecombat.com/u/Wesley_Leung)\
**Post date:** [January 29, 2021, 1:07am UTC](https://discourse.codecombat.com/t/hunting-party-coding-help/27107/12 "2021-01-29T01:07:23Z")

</div>

```python
while True:
    friends = hero.findFriends()
    # Use for-loop and for each friend:
    for i in range(len(friends)):
        # If they see an enemy then command to attack.
        friend = friends[i]
        enemy = friend.findNearestEnemy
        if enemy:
            hero.command(friend, "attack", enemy)
            # Command to move east by small steps.
        else:
            y = friend.pos.y
            x = friend.pos.x + 10
            hero.command(friend, "move", {"x": x, "y": y})

```

AND THEN IT HAS A ERROR SAYING THAT ENEMY IS A FUNCTION

---

<div class="post-metadata">

**Author:** ![Grzmot](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/grzmot/32/13514_2.png) [@Grzmot](https://discourse.codecombat.com/u/Grzmot)\
**Post date:** [January 29, 2021, 1:09am UTC](https://discourse.codecombat.com/t/hunting-party-coding-help/27107/13 "2021-01-29T01:09:54Z")

</div>

> [@Wesley\_Leung](#):
>
> `enemy = friend.findNearestEnemy`

Forgot the parenthesis 🙃

---

<div class="post-metadata">

**Author:** ![Wesley\_Leung](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/wesley_leung/32/13935_2.png) [@Wesley\_Leung](https://discourse.codecombat.com/u/Wesley_Leung)\
**Post date:** [January 29, 2021, 1:10am UTC](https://discourse.codecombat.com/t/hunting-party-coding-help/27107/14 "2021-01-29T01:10:53Z")

</div>

oooof (20 characters)

---

<div class="post-metadata">

**Author:** ![Grzmot](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/grzmot/32/13514_2.png) [@Grzmot](https://discourse.codecombat.com/u/Grzmot)\
**Post date:** [January 29, 2021, 1:11am UTC](https://discourse.codecombat.com/t/hunting-party-coding-help/27107/15 "2021-01-29T01:11:11Z")

</div>

Does it work now?  
-Grzmot

---

<div class="post-metadata">

**Author:** ![Wesley\_Leung](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/wesley_leung/32/13935_2.png) [@Wesley\_Leung](https://discourse.codecombat.com/u/Wesley_Leung)\
**Post date:** [January 29, 2021, 1:11am UTC](https://discourse.codecombat.com/t/hunting-party-coding-help/27107/16 "2021-01-29T01:11:58Z")

</div>

yes, it works perfectly

---

<div class="post-metadata">

**Author:** ![Wesley\_Leung](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/wesley_leung/32/13935_2.png) [@Wesley\_Leung](https://discourse.codecombat.com/u/Wesley_Leung)\
**Post date:** [January 29, 2021, 1:12am UTC](https://discourse.codecombat.com/t/hunting-party-coding-help/27107/17 "2021-01-29T01:12:21Z")

</div>

but too bad the bonus is hard to achieve

---

<div class="post-metadata">

**Author:** ![Grzmot](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/grzmot/32/13514_2.png) [@Grzmot](https://discourse.codecombat.com/u/Grzmot)\
**Post date:** [January 29, 2021, 1:14am UTC](https://discourse.codecombat.com/t/hunting-party-coding-help/27107/18 "2021-01-29T01:14:33Z")

</div>

Bonus isn’t too hard to actually get, try adjusting this number:

> [@Wesley\_Leung](#):
>
> `x = friend.pos.x + 10`

(Hint: it should be lower than 10)  
Hope this helps!  
-Grzmot

---

<div class="post-metadata">

**Author:** ![Wesley\_Leung](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/wesley_leung/32/13935_2.png) [@Wesley\_Leung](https://discourse.codecombat.com/u/Wesley_Leung)\
**Post date:** [January 29, 2021, 1:15am UTC](https://discourse.codecombat.com/t/hunting-party-coding-help/27107/19 "2021-01-29T01:15:39Z")

</div>

I did 5 and it did not work…

---

<div class="post-metadata">

**Author:** ![Grzmot](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/grzmot/32/13514_2.png) [@Grzmot](https://discourse.codecombat.com/u/Grzmot)\
**Post date:** [January 29, 2021, 1:16am UTC](https://discourse.codecombat.com/t/hunting-party-coding-help/27107/20 "2021-01-29T01:16:44Z")

</div>

Try for lower… If you don’t succeed the first time try again  
-Grzmot

[Next page](https://discourse.codecombat.com/t/hunting-party-coding-help/27107.md?page=2)
