# Hunting Party - stuck! (Python)

**URL:** <https://discourse.codecombat.com/t/hunting-party-stuck-python/13251>\
**Category:** Level Help\
**Created:** [December 14, 2017, 2:25am UTC](https://discourse.codecombat.com/t/hunting-party-stuck-python/13251 "2017-12-14T02:25:56Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ian\_Lambert-Brown](https://avatars.discourse-cdn.com/v4/letter/i/96bed5/32.png) [@Ian\_Lambert-Brown](https://discourse.codecombat.com/u/Ian_Lambert-Brown)\
**Post date:** [December 14, 2017, 2:25am UTC](https://discourse.codecombat.com/t/hunting-party-stuck-python/13251/1 "2017-12-14T02:25:56Z")

</div>

Okay so here is my code

```python
friends = hero.findFriends()

for friend in friends:
    enemy = friend.findNearest(friend.findEnemies())
    if not enemy:
        hero.command(friend, "move", {'x': friend.pos.x + 24, 'y': friend.pos.y})
    else:
        hero.command(friend, "attack", enemy)

```

I know the for loop is going to loop over each friend. I also tried it this way:

```python
friends = hero.findFriends()

for friend in friends:
    enemy = friend.findNearest(friend.findEnemies())
    if enemy:
        hero.command(friend, "attack", enemy)
    else:
        hero.command(friend, "move", {'x': friend.pos.x + 24, 'y': friend.pos.y})

```

In each case all of my units move 24 units to the right and then the top units stand there and get attacked. I’m not sure what to do and unfortunately I’m really lost. I can’t figure out exactly what it’s asking me and the hints aren’t helping. Please advise.

---

<div class="post-metadata">

**Author:** ![MunkeyShynes](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/munkeyshynes/32/9277_2.png) [@MunkeyShynes](https://discourse.codecombat.com/u/MunkeyShynes)\
**Post date:** [December 14, 2017, 2:34am UTC](https://discourse.codecombat.com/t/hunting-party-stuck-python/13251/2 "2017-12-14T02:34:29Z")

</div>

From the hints:  
 ![HuntParty01](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/2X/1/1b7f25d7442b58311bbf9fa02bcdd39cd0f35b69.JPG)

24 is WAY too much. Try putting a decimal point in front of that value. Then keep adjusting the number until it works.

BTW, your second code post should work, IF you put the whole thing (all of it) in a loop.

---

<div class="post-metadata">

**Author:** ![Ian\_Lambert-Brown](https://avatars.discourse-cdn.com/v4/letter/i/96bed5/32.png) [@Ian\_Lambert-Brown](https://discourse.codecombat.com/u/Ian_Lambert-Brown)\
**Post date:** [December 14, 2017, 2:36am UTC](https://discourse.codecombat.com/t/hunting-party-stuck-python/13251/3 "2017-12-14T02:36:46Z")

</div>

All that happens when I change it is they move forward less and then stop.

---

<div class="post-metadata">

**Author:** ![MunkeyShynes](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/munkeyshynes/32/9277_2.png) [@MunkeyShynes](https://discourse.codecombat.com/u/MunkeyShynes)\
**Post date:** [December 14, 2017, 2:38am UTC](https://discourse.codecombat.com/t/hunting-party-stuck-python/13251/4 "2017-12-14T02:38:19Z")

</div>

it stops because it’s not looping through the code. There is no loop. Try while True for the whole thing.

---

<div class="post-metadata">

**Author:** ![Ian\_Lambert-Brown](https://avatars.discourse-cdn.com/v4/letter/i/96bed5/32.png) [@Ian\_Lambert-Brown](https://discourse.codecombat.com/u/Ian_Lambert-Brown)\
**Post date:** [December 14, 2017, 2:40am UTC](https://discourse.codecombat.com/t/hunting-party-stuck-python/13251/5 "2017-12-14T02:40:19Z")

</div>

That’s what I just did. I saw you adjust your earlier comment. That seems to be working but it’s being really laggy. Well, some troops died, but the while True loop fixed it. Thank you so much. I feel like I’m losing my mind today and this level was just the icing on the cake.

---

<div class="post-metadata">

**Author:** ![MunkeyShynes](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/munkeyshynes/32/9277_2.png) [@MunkeyShynes](https://discourse.codecombat.com/u/MunkeyShynes)\
**Post date:** [December 14, 2017, 2:42am UTC](https://discourse.codecombat.com/t/hunting-party-stuck-python/13251/6 "2017-12-14T02:42:04Z")

</div>

now adjust that numeric value until it works and no one dies so you get the bonus.

---

<div class="post-metadata">

**Author:** ![KALLEE\_SMITH\_Student](https://avatars.discourse-cdn.com/v4/letter/k/e480ec/32.png) [@KALLEE\_SMITH\_Student](https://discourse.codecombat.com/u/KALLEE_SMITH_Student)\
**Post date:** [December 14, 2017, 2:32pm UTC](https://discourse.codecombat.com/t/hunting-party-stuck-python/13251/7 "2017-12-14T14:32:53Z")

</div>

What do you want? Mk see ya.

---

<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 21, 2019, 11:46pm UTC](https://discourse.codecombat.com/t/hunting-party-stuck-python/13251/8 "2019-01-21T23:46:58Z")

</div>

I’m having trouble with this level. My troops move right, but when they see an enemy they don’t attack and actually move left. Here is my code:

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

```

---

<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:** [January 22, 2019, 12:10am UTC](https://discourse.codecombat.com/t/hunting-party-stuck-python/13251/9 "2019-01-22T00:10:52Z")

</div>

I don’t see nothing wrong with your code. Increase the value to 0.3 or 0.45 and submit several times.

---

<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, 12:41am UTC](https://discourse.codecombat.com/t/hunting-party-stuck-python/13251/10 "2019-01-22T00:41:27Z")

</div>

I tried changing the number, but the troops still don’t attack.

---

<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:** [January 22, 2019, 1:08am UTC](https://discourse.codecombat.com/t/hunting-party-stuck-python/13251/11 "2019-01-22T01:08:23Z")

</div>

OMG 🙂 Didn’t see that!  
You have:

```python
while True:
    friends = hero.findFriends()
    enemy = hero.findNearestEnemy()
   hero.say(enemy.id)
    # and so on... 

```

 ![hunting](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/2X/6/646883fa65ed56ccca4f6decce93141ef97cc0d9.jpeg)  
Do you see the one on the top right corner 🙂  
put:

```python
    for friend in friends:
        # your friend must find the enemy

```

---

<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, 1:29am UTC](https://discourse.codecombat.com/t/hunting-party-stuck-python/13251/12 "2019-01-22T01:29:42Z")

</div>

What? I don’t really understand.

---

<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:** [January 22, 2019, 1:42am UTC](https://discourse.codecombat.com/t/hunting-party-stuck-python/13251/13 "2019-01-22T01:42:46Z")

</div>

All your warriors are attacking the enemy that’s closer to the hero - Pinakin. Every warrior must attack the enemy next to him. The first poster - Ian\_Lambert-BrownBrown has done it right, ( I have put hero.say(enemy.id) for debug

---

<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, 1:51am UTC](https://discourse.codecombat.com/t/hunting-party-stuck-python/13251/14 "2019-01-22T01:51:22Z")

</div>

What item allows “friend.findNearest(friend.findEnemies())” because I don’t have that, or is it already allowed?

---

<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:** [January 22, 2019, 2:01am UTC](https://discourse.codecombat.com/t/hunting-party-stuck-python/13251/15 "2019-01-22T02:01:18Z")

</div>

This is same as friend.findNearestEnemy()

---

<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, 2:06am UTC](https://discourse.codecombat.com/t/hunting-party-stuck-python/13251/16 "2019-01-22T02:06:56Z")

</div>

It says it’s an error.

---

<div class="post-metadata">

**Author:** ![MunkeyShynes](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/munkeyshynes/32/9277_2.png) [@MunkeyShynes](https://discourse.codecombat.com/u/MunkeyShynes)\
**Post date:** [January 22, 2019, 2:20am UTC](https://discourse.codecombat.com/t/hunting-party-stuck-python/13251/17 "2019-01-22T02:20:50Z")

</div>

It says it’s an error because you put the definition of `enemy = friend.findNearestEnemy()` before you defined friend in the for loop. Put the enemy definition inside the for loop.

---

<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, 2:24am UTC](https://discourse.codecombat.com/t/hunting-party-stuck-python/13251/18 "2019-01-22T02:24:36Z")

</div>

Thanks for the advice! It works fine now.

---

<div class="post-metadata">

**Author:** ![Picsix](https://avatars.discourse-cdn.com/v4/letter/p/958977/32.png) [@Picsix](https://discourse.codecombat.com/u/Picsix)\
**Post date:** [December 5, 2019, 9:52pm UTC](https://discourse.codecombat.com/t/hunting-party-stuck-python/13251/19 "2019-12-05T21:52:47Z")

</div>

What is the enemy definition

---

<div class="post-metadata">

**Author:** ![JamesClanCodeMaster](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/jamesclancodemaster/32/11759_2.png) [@JamesClanCodeMaster](https://discourse.codecombat.com/u/JamesClanCodeMaster)\
**Post date:** [April 5, 2020, 5:42pm UTC](https://discourse.codecombat.com/t/hunting-party-stuck-python/13251/20 "2020-04-05T17:42:41Z")

</div>

I need some help, here is my code

```python
# You can use findNearestEnemy() on your soldiers to get their nearest enemy instead of yours.
while True:
    friends = hero.findFriends()
    # Use for-loop and for each friend:
    for friend in friends:
        # If they see an enemy then command to attack.
        enemy = friend.findEnemies(friend.findNearest())
        if enemy:
            hero.command(friend, "attack", enemy)
        # Command to move east by small steps.
        else:
            hero.command(friend, "move", {"x": friend.pos.x + 5, "y": friend.pos.y})

```

[Next page](https://discourse.codecombat.com/t/hunting-party-stuck-python/13251.md?page=2)
