# (Python): Hunter's Prey

**URL:** <https://discourse.codecombat.com/t/python-hunters-prey/26710>\
**Category:** Level Help\
**Created:** [January 16, 2021, 12:50am UTC](https://discourse.codecombat.com/t/python-hunters-prey/26710 "2021-01-16T00:50:02Z")\
**Posts on this page:** 20\
**Page:** 2

<div class="post-metadata">

**Author:** ![Eric\_Tang](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/eric_tang/32/15310_2.png) [@Eric\_Tang](https://discourse.codecombat.com/u/Eric_Tang)\
**Post date:** [January 30, 2021, 12:33am UTC](https://discourse.codecombat.com/t/python-hunters-prey/26710/22 "2021-01-30T00:33:19Z")

</div>

Alright y’alls are getting #off-topic let’s start helping @phantomofthedark2

---

<div class="post-metadata">

**Author:** ![phantomofthedark2](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/phantomofthedark2/32/17807_2.png) [@phantomofthedark2](https://discourse.codecombat.com/u/phantomofthedark2)\
**Post date:** [February 1, 2021, 2:23pm UTC](https://discourse.codecombat.com/t/python-hunters-prey/26710/23 "2021-02-01T14:23:51Z")

</div>

yes please.(20210)))

---

<div class="post-metadata">

**Author:** ![milton.jinich](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/milton.jinich/32/21419_2.png) [@milton.jinich](https://discourse.codecombat.com/u/milton.jinich)\
**Post date:** [February 1, 2021, 2:35pm UTC](https://discourse.codecombat.com/t/python-hunters-prey/26710/24 "2021-02-01T14:35:31Z")

</div>

So what is your new code

---

<div class="post-metadata">

**Author:** ![phantomofthedark2](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/phantomofthedark2/32/17807_2.png) [@phantomofthedark2](https://discourse.codecombat.com/u/phantomofthedark2)\
**Post date:** [February 1, 2021, 2:52pm UTC](https://discourse.codecombat.com/t/python-hunters-prey/26710/25 "2021-02-01T14:52:44Z")

</div>

This is my class break. I cant untill 12:00

---

<div class="post-metadata">

**Author:** ![phantomofthedark2](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/phantomofthedark2/32/17807_2.png) [@phantomofthedark2](https://discourse.codecombat.com/u/phantomofthedark2)\
**Post date:** [February 1, 2021, 3:23pm UTC](https://discourse.codecombat.com/t/python-hunters-prey/26710/26 "2021-02-01T15:23:22Z")

</div>

Ok. This is my new 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():
    friends = hero.findFriends()
    for soldier in friends:
        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():
    friends = hero.findByType("archer", hero.findFriends())
    for friend in friends:
        enemy = friend.findNearestEnemy()
        if enemy:
            if hero.distanceTo(enemy) <= 25:
                
                if friend.type == "archer":
                    hero.command(friend, "attack", enemy)
                
            else:
                hero.command(friend, "move", {'x':24, 'y':47})

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:** ![milton.jinich](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/milton.jinich/32/21419_2.png) [@milton.jinich](https://discourse.codecombat.com/u/milton.jinich)\
**Post date:** [February 1, 2021, 3:26pm UTC](https://discourse.codecombat.com/t/python-hunters-prey/26710/27 "2021-02-01T15:26:26Z")

</div>

Okay try changing this

> [@phantomofthedark2](#):
>
> `{'x':24, 'y':47}`

Into

```python
friend.pos

```

---

<div class="post-metadata">

**Author:** ![phantomofthedark2](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/phantomofthedark2/32/17807_2.png) [@phantomofthedark2](https://discourse.codecombat.com/u/phantomofthedark2)\
**Post date:** [February 1, 2021, 3:29pm UTC](https://discourse.codecombat.com/t/python-hunters-prey/26710/28 "2021-02-01T15:29:19Z")

</div>

the same thing happens. This is the pic  
 ![image](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/3X/9/4/944df588cea05b89bdf16e0a3917bfb3865aed3d.jpeg)

---

<div class="post-metadata">

**Author:** ![milton.jinich](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/milton.jinich/32/21419_2.png) [@milton.jinich](https://discourse.codecombat.com/u/milton.jinich)\
**Post date:** [February 1, 2021, 3:31pm UTC](https://discourse.codecombat.com/t/python-hunters-prey/26710/29 "2021-02-01T15:31:22Z")

</div>

This is the problem change that into this `>=`

> [@phantomofthedark2](#):
>
> `<=`

---

<div class="post-metadata">

**Author:** ![phantomofthedark2](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/phantomofthedark2/32/17807_2.png) [@phantomofthedark2](https://discourse.codecombat.com/u/phantomofthedark2)\
**Post date:** [February 1, 2021, 3:33pm UTC](https://discourse.codecombat.com/t/python-hunters-prey/26710/30 "2021-02-01T15:33:05Z")

</div>

The same thing happens 😭  
Here is the 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():
    friends = hero.findFriends()
    for soldier in friends:
        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():
    friends = hero.findByType("archer", hero.findFriends())
    for friend in friends:
        enemy = friend.findNearestEnemy()
        if enemy:
            if hero.distanceTo(enemy) >= 25:
                
                if friend.type == "archer":
                    hero.command(friend, "attack", enemy)
                
            else:
                hero.command(friend, "move", friend.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(friend)
            pass

```

---

<div class="post-metadata">

**Author:** ![phantomofthedark2](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/phantomofthedark2/32/17807_2.png) [@phantomofthedark2](https://discourse.codecombat.com/u/phantomofthedark2)\
**Post date:** [February 1, 2021, 3:54pm UTC](https://discourse.codecombat.com/t/python-hunters-prey/26710/31 "2021-02-01T15:54:32Z")

</div>

Anybody here? 😱

---

<div class="post-metadata">

**Author:** ![Deadpool198](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/deadpool198/32/16753_2.png) [@Deadpool198](https://discourse.codecombat.com/u/Deadpool198)\
**Post date:** [February 1, 2021, 4:32pm UTC](https://discourse.codecombat.com/t/python-hunters-prey/26710/32 "2021-02-01T16:32:18Z")

</div>

Firstly: change the \>= back to \<=. You only want to attack when the ogres are closer than 25 meters, so you want to wait until the distance is less than or equal to (\<=) 25.  
However, the main problem is the fact that you’re using two for loops. One in the while true loop to isolate an individual soldier, which you then put into the commandSoldier and commandArcher functions, and a second for loop inside those functions. This isn’t going to work that well.  
Inside the commandSoldier and commandArcher functions, you don’t need another for loop. You need to have an archer and soldier (respectively) variable inside the brackets of the function like:  
def commandSoldier(soldier):  
Then you command **one individual** soldier, you don’t make another for loop. The for loop in the while true loop has already singled out **one individual** soldier, who you can then command with the same code you have in the function, _just without the for loop_.  
Does that make any sense? Just remove the for loops from inside the commanding functions, and put archer and soldier variables inside the brackets of those functions.  
Danny

---

<div class="post-metadata">

**Author:** ![phantomofthedark2](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/phantomofthedark2/32/17807_2.png) [@phantomofthedark2](https://discourse.codecombat.com/u/phantomofthedark2)\
**Post date:** [February 1, 2021, 4:41pm UTC](https://discourse.codecombat.com/t/python-hunters-prey/26710/33 "2021-02-01T16:41:45Z")

</div>

Can you simplify it?

---

<div class="post-metadata">

**Author:** ![phantomofthedark2](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/phantomofthedark2/32/17807_2.png) [@phantomofthedark2](https://discourse.codecombat.com/u/phantomofthedark2)\
**Post date:** [February 1, 2021, 4:57pm UTC](https://discourse.codecombat.com/t/python-hunters-prey/26710/34 "2021-02-01T16:57:26Z")

</div>

IS anybody here(2021)

---

<div class="post-metadata">

**Author:** ![Falcons118](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/falcons118/32/32017_2.png) [@Falcons118](https://discourse.codecombat.com/u/Falcons118)\
**Post date:** [February 1, 2021, 4:59pm UTC](https://discourse.codecombat.com/t/python-hunters-prey/26710/35 "2021-02-01T16:59:04Z")

</div>

just read what deadpool said.  
if its confusing break it up into steps

---

<div class="post-metadata">

**Author:** ![phantomofthedark2](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/phantomofthedark2/32/17807_2.png) [@phantomofthedark2](https://discourse.codecombat.com/u/phantomofthedark2)\
**Post date:** [February 1, 2021, 5:00pm UTC](https://discourse.codecombat.com/t/python-hunters-prey/26710/36 "2021-02-01T17:00:08Z")

</div>

Ok (2021202012020120201)

---

<div class="post-metadata">

**Author:** ![Falcons118](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/falcons118/32/32017_2.png) [@Falcons118](https://discourse.codecombat.com/u/Falcons118)\
**Post date:** [February 1, 2021, 5:00pm UTC](https://discourse.codecombat.com/t/python-hunters-prey/26710/37 "2021-02-01T17:00:56Z")

</div>

(credit to deadpool btw)  
First:

> [@Deadpool198](#):
>
> Firstly: change the \>= back to \<=. You only want to attack when the ogres are closer than 25 meters, so you want to wait until the distance is less than or equal to (\<=) 25.

Second:

> [@Deadpool198](#):
>
> Inside the commandSoldier and commandArcher functions, you don’t need another for loop. You need to have an archer and soldier (respectively) variable inside the brackets of the function like:  
> def commandSoldier(soldier):

3rd:

> [@Deadpool198](#):
>
> Then you command **one individual** soldier, you don’t make another for loop. The for loop in the while true loop has already singled out **one individual** soldier, who you can then command with the same code you have in the function, _just without the for loop_ .

summary

> [@Deadpool198](#):
>
> Just remove the for loops from inside the commanding functions, and put archer and soldier variables inside the brackets of those functions.

just do all this in these steps. (If this works remember to give deadpool the solution thing since he wrote all of this)

---

<div class="post-metadata">

**Author:** ![phantomofthedark2](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/phantomofthedark2/32/17807_2.png) [@phantomofthedark2](https://discourse.codecombat.com/u/phantomofthedark2)\
**Post date:** [February 1, 2021, 5:04pm UTC](https://discourse.codecombat.com/t/python-hunters-prey/26710/38 "2021-02-01T17:04:40Z")

</div>

Thank you so much! I have bin stuck since my bday

---

<div class="post-metadata">

**Author:** ![Luke10](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/luke10/32/19892_2.png) [@Luke10](https://discourse.codecombat.com/u/Luke10)\
**Post date:** [February 1, 2021, 5:05pm UTC](https://discourse.codecombat.com/t/python-hunters-prey/26710/39 "2021-02-01T17:05:51Z")

</div>

Good stuff Deadpool 🙂 bro all of yall are so much smarter than me lol

---

<div class="post-metadata">

**Author:** ![Falcons118](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/falcons118/32/32017_2.png) [@Falcons118](https://discourse.codecombat.com/u/Falcons118)\
**Post date:** [February 1, 2021, 5:07pm UTC](https://discourse.codecombat.com/t/python-hunters-prey/26710/40 "2021-02-01T17:07:37Z")

</div>

anytime 😁  
thanks for helping deadpool

---

<div class="post-metadata">

**Author:** ![Luke10](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/luke10/32/19892_2.png) [@Luke10](https://discourse.codecombat.com/u/Luke10)\
**Post date:** [February 1, 2021, 5:08pm UTC](https://discourse.codecombat.com/t/python-hunters-prey/26710/41 "2021-02-01T17:08:14Z")

</div>

You too tho Falcon you’re one of the smartest pple here

[Previous page](https://discourse.codecombat.com/t/python-hunters-prey/26710.md?page=1)

[Next page](https://discourse.codecombat.com/t/python-hunters-prey/26710.md?page=3)
