# Hunting Party Help!

**URL:** <https://discourse.codecombat.com/t/hunting-party-help/6510>\
**Category:** Uncategorized\
**Created:** [January 19, 2016, 9:12pm UTC](https://discourse.codecombat.com/t/hunting-party-help/6510 "2016-01-19T21:12:30Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Thelightside](https://avatars.discourse-cdn.com/v4/letter/t/b782af/32.png) [@Thelightside](https://discourse.codecombat.com/u/Thelightside)\
**Post date:** [January 19, 2016, 9:12pm UTC](https://discourse.codecombat.com/t/hunting-party-help/6510/1 "2016-01-19T21:12:30Z")

</div>

This error is really weird since I gave this code to my friends and it worked fine.  
The Code:

```python
# Command your troops to move east and attack any ogres they see.
# Use for-loops and findFriends.
# You can use findNearestEnemy() on your soldiers to get their nearest enemy instead of yours.
self.moveXY(84, 48)
self.moveXY(61, 28)
self.moveXY(58, 67)
loop:
    enemy = self.findNearest(self.findEnemies())
    if enemy:
        self.attack(enemy)

```

The outcome is Ran Out of Time.

---

<div class="post-metadata">

**Author:** ![AdrianCgw](https://avatars.discourse-cdn.com/v4/letter/a/ac8455/32.png) [@AdrianCgw](https://discourse.codecombat.com/u/AdrianCgw)\
**Post date:** [January 19, 2016, 10:15pm UTC](https://discourse.codecombat.com/t/hunting-party-help/6510/2 "2016-01-19T22:15:27Z")

</div>

Your code does not command your soldiers, only your hero.  
It can win the level if you have a hero with really powerful weapons.  
If no powerful hero, it will fail because the hero will take too long to kill everyone all by himself.

Go back to earlier levels and see how you command your troops

If you still need help, post your new code and we’ll help you fix it.

---

<div class="post-metadata">

**Author:** ![Thelightside](https://avatars.discourse-cdn.com/v4/letter/t/b782af/32.png) [@Thelightside](https://discourse.codecombat.com/u/Thelightside)\
**Post date:** [January 19, 2016, 10:19pm UTC](https://discourse.codecombat.com/t/hunting-party-help/6510/3 "2016-01-19T22:19:16Z")

</div>

Actually All my soilders go and attack.I will command.

---

<div class="post-metadata">

**Author:** ![Thelightside](https://avatars.discourse-cdn.com/v4/letter/t/b782af/32.png) [@Thelightside](https://discourse.codecombat.com/u/Thelightside)\
**Post date:** [January 19, 2016, 10:28pm UTC](https://discourse.codecombat.com/t/hunting-party-help/6510/4 "2016-01-19T22:28:17Z")

</div>

```python
self.moveXY(84, 48)
self.moveXY(61, 28)
self.moveXY(58, 67)
loop:
    enemy = self.findNearest(self.findEnemies())
    if enemy:
        self.attack(enemy)
    if friend:
        self.command(soilder, "attack")

```

does not work

---

<div class="post-metadata">

**Author:** ![AdrianCgw](https://avatars.discourse-cdn.com/v4/letter/a/ac8455/32.png) [@AdrianCgw](https://discourse.codecombat.com/u/AdrianCgw)\
**Post date:** [January 19, 2016, 10:40pm UTC](https://discourse.codecombat.com/t/hunting-party-help/6510/5 "2016-01-19T22:40:26Z")

</div>

And how you get the value of soldier?  
You can get all your friends with:

```python
soldiers=self.findFriends()

```

but then how you iterate (go over each solider in soldiers)?

---

<div class="post-metadata">

**Author:** ![Thelightside](https://avatars.discourse-cdn.com/v4/letter/t/b782af/32.png) [@Thelightside](https://discourse.codecombat.com/u/Thelightside)\
**Post date:** [January 19, 2016, 11:31pm UTC](https://discourse.codecombat.com/t/hunting-party-help/6510/6 "2016-01-19T23:31:19Z")

</div>

Where do I put solider in soilders???

---

<div class="post-metadata">

**Author:** ![AdrianCgw](https://avatars.discourse-cdn.com/v4/letter/a/ac8455/32.png) [@AdrianCgw](https://discourse.codecombat.com/u/AdrianCgw)\
**Post date:** [January 19, 2016, 11:39pm UTC](https://discourse.codecombat.com/t/hunting-party-help/6510/7 "2016-01-19T23:39:25Z")

</div>

Commanding the soldiers does not take game it. Everything else your hero does takes game time, so command your soldiers first:

```python
loop:
    #command soldiers
    #command hero

```

you already have the command hero part in the loop

If you are not sure how to command each soldier in soldiers go back to earlier levels for example, or see  
the discussion on Mountain Mercenaries for how to give orders to each soldier:

> [@Mounatin Mercenaries Error](https://discourse.codecombat.com/t/mounatin-mercenaries-error/6174/22):
>
> # Gather coins to summon soldiers and have them attack the enemy loop: items = self.findItems() item = self.findNearest(items) itemPos = item.pos x = itemPos.x y = itemPos.y # Move to the nearest coin. # Use move instead of moveXY so you can command constantly. self.moveXY(x, y) # If you have funds for a soldier, summon one. if self.gold \> self.costOf("soldier"): self.summon("soldier") enemies = self.findEnemies() enemy = self.findNeares…

---

<div class="post-metadata">

**Author:** ![Thelightside](https://avatars.discourse-cdn.com/v4/letter/t/b782af/32.png) [@Thelightside](https://discourse.codecombat.com/u/Thelightside)\
**Post date:** [January 20, 2016, 1:08am UTC](https://discourse.codecombat.com/t/hunting-party-help/6510/8 "2016-01-20T01:08:04Z")

</div>

I will be working on it.😌

---

<div class="post-metadata">

**Author:** ![Thelightside](https://avatars.discourse-cdn.com/v4/letter/t/b782af/32.png) [@Thelightside](https://discourse.codecombat.com/u/Thelightside)\
**Post date:** [January 20, 2016, 1:37am UTC](https://discourse.codecombat.com/t/hunting-party-help/6510/9 "2016-01-20T01:37:40Z")

</div>

It still does not work!!!

```python
# Command your troops to move east and attack any ogres they see.
# Use for-loops and findFriends.
# You can use findNearestEnemy() on your soldiers to get their nearest enemy instead of yours.
self.moveXY(84, 48)
self.moveXY(61, 28)
self.moveXY(58, 67)
loop:
    enemy = self.findNearest(self.findEnemies())
    friend = self.findFriends()
    if enemy:
        self.attack(enemy)
    for soilder in soilders:
        self.command(soilder, "attack", enemy)

```

---

<div class="post-metadata">

**Author:** ![AdrianCgw](https://avatars.discourse-cdn.com/v4/letter/a/ac8455/32.png) [@AdrianCgw](https://discourse.codecombat.com/u/AdrianCgw)\
**Post date:** [January 20, 2016, 4:22am UTC](https://discourse.codecombat.com/t/hunting-party-help/6510/10 "2016-01-20T04:22:44Z")

</div>

“Still does not work” does not give any information. Usually we do not have time or possibility to run your code. Even if we can run it we do not know what you actually intended to do.

So

**1) If there is an error, tells us the error and mark the line in your code.**  
If it runs but do not do what you wanted, tell us  
**2) What are you seeing**  
**3) what are you expecting to see**

I’ll teach you to debug the code. Teach a man to finish, …

Get a white piece of paper and start with the first line.  
The paper will mark everything that the code interpreter know

First line 1) loop – KNOWN: nothing, code interpreter does not know a thing

```python
2) *enemy =self.findNearest(self.findEnemies())*

```

KNOWN: enemy - **hero’s** nearest enemy, possibly empty

**Remember the possibly empty notation**. Always assume that the values that you are getting are junk or latter in life, malicious inputs intended to break down your site or application 🙂

```python
3) friend = self.findFriends()

```

KNOWN:  
enemy -hero’s nearest, possibly empty  
friend - list of soldiers, possibly empty

```python
4) (1 out of 2) if enemy:

```

During each if there a two paths to take: case when the condition is true and the case when the condition is false.  
Each extra if adds another path. If there are 3 ifs one inside the other, the whole block has 4 different paths you need to consider

If you are taking a path, then you add the condition=true to the list of things the code interpreter knows at that time

```python
5) self.attack(enemy)

```

This is inside the if so it means that **enemy is not empty**  
if (value) is the standard way of checking if a value is not empty.  
KNOWNS: nemy, hero’s nearest, \*_NOT empty_  
_friends: list of soldiers, possibly empty_

self.attack needs a **not empty enemy** value so it is ok 🙂

1. (2 out of 2) There is no else branch for the if so we do nothing. When going over an “if” always count how many branches you need to go over and ask yourself: it is ok to ignore the missing branches?  
In this case the branch is no enemy, and it may be ok for the hero to do nothing if there are no enemies around

Once you finish the if, you drop the knowledge from the if condition so for now on enemy can be empty again.

```python
6) for soilder in soilders:

```

This will go over each soilder in the soilders list. It will be ok if the list is empty  
KNOWS: _enemy, heroes nearest, possibly empty_ (we are now outside the if so we have to drop the condition=true assumption.  
_friends - list of soliders, possibly empty_

_for soilder in soilders_ needs a **soilders list-type value**. The interpreter does not know it at the moment so **BAM** , your code fails.

```python
7) self.command(soilder, "attack", enemy)

```

Once you fix the code and reach here,  
KNOW: enemy, hero’s nearest, possibly empty  
soilder - an item from the list soilders, not empty  
friends - soldiers list, possibly empty

Soilder is know not empty because you got it from _for soilder in_ beforehand.  
if you will get the soliders list each loop using findFriends, then it will not contain empty value.  
But on a more general case you could place an if(soilder) to make sure soilder is not empty

_Self.command_ needs:  
soilder - not empty  
enemy - not empty  
If you do not have any of this, then you’ll get an error again

==================================================================

Finally, the level asks your soldiers to attack their nearest enemy not yours. Their nearest enemy for a soldier _soldy_ can be got with:

```python
soldy_nearest =soldy.findNearestEnemy()

```

This requires soldy to be known, not empty.  
The result soldy\_nearest will be known but possibly empty

---

<div class="post-metadata">

**Author:** ![Steven\_Kang2003](https://avatars.discourse-cdn.com/v4/letter/s/7ea924/32.png) [@Steven\_Kang2003](https://discourse.codecombat.com/u/Steven_Kang2003)\
**Post date:** [January 21, 2016, 7:46pm UTC](https://discourse.codecombat.com/t/hunting-party-help/6510/11 "2016-01-21T19:46:09Z")

</div>

i just lured all the ogres into the archers and did not attack  
i recommend speed ring and softened leather boots when you are doing this and decent gear:slightly\_smiling:
