# Timber Guard(python)

**URL:** <https://discourse.codecombat.com/t/timber-guard-python/24094>\
**Category:** Level Help\
**Created:** [September 14, 2020, 1:24am UTC](https://discourse.codecombat.com/t/timber-guard-python/24094 "2020-09-14T01:24:29Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![CocoCharlie](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/cococharlie/32/19081_2.png) [@CocoCharlie](https://discourse.codecombat.com/u/CocoCharlie)\
**Post date:** [September 14, 2020, 1:24am UTC](https://discourse.codecombat.com/t/timber-guard-python/24094/1 "2020-09-14T01:24:29Z")

</div>

I don’t know what to do next.

```python
while True:
    coin = hero.findNearestItem()
    if coin:
        hero.move(coin.pos)
    # If you have enough gold, summon a soldier.
    if hero.gold > hero.costOf("soldier"):
        hero.summon("soldier")
    # Collect gold.
    # Use a for-loop to command each soldier.
    # For loops have two parts: "for X in Y"
    # Y is the array to loop over.
    # The loop will run once for each item in Y, with X set to the current item.
    for friend in hero.findFriends():
        if friend.type == "soldier":
            enemy = friend.findNearestEnemy()
            # If there's an enemy, command her to attack.
            # Otherwise, move her to the right side of the map.
            
            

```

---

<div class="post-metadata">

**Author:** ![TheOrbitals](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/theorbitals/32/28144_2.png) [@TheOrbitals](https://discourse.codecombat.com/u/TheOrbitals)\
**Post date:** [September 14, 2020, 1:36am UTC](https://discourse.codecombat.com/t/timber-guard-python/24094/2 "2020-09-14T01:36:06Z")

</div>

Hmm where is this level at? (probably mountain)  
~ Orb

---

<div class="post-metadata">

**Author:** ![TheOrbitals](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/theorbitals/32/28144_2.png) [@TheOrbitals](https://discourse.codecombat.com/u/TheOrbitals)\
**Post date:** [September 14, 2020, 1:36am UTC](https://discourse.codecombat.com/t/timber-guard-python/24094/3 "2020-09-14T01:36:44Z")

</div>

And I just reseted my progress to do C++ (I still can do Python), please give me a screenshot of your code, the scene, and the goals together  
~ Orb

---

<div class="post-metadata">

**Author:** ![CocoCharlie](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/cococharlie/32/19081_2.png) [@CocoCharlie](https://discourse.codecombat.com/u/CocoCharlie)\
**Post date:** [September 14, 2020, 1:36am UTC](https://discourse.codecombat.com/t/timber-guard-python/24094/4 "2020-09-14T01:36:56Z")

</div>

Yes. a little ways into the mountain.

---

<div class="post-metadata">

**Author:** ![CocoCharlie](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/cococharlie/32/19081_2.png) [@CocoCharlie](https://discourse.codecombat.com/u/CocoCharlie)\
**Post date:** [September 14, 2020, 1:37am UTC](https://discourse.codecombat.com/t/timber-guard-python/24094/5 "2020-09-14T01:37:44Z")

</div>

Okay hold on a sec.(200000000000char)

---

<div class="post-metadata">

**Author:** ![CocoCharlie](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/cococharlie/32/19081_2.png) [@CocoCharlie](https://discourse.codecombat.com/u/CocoCharlie)\
**Post date:** [September 14, 2020, 1:38am UTC](https://discourse.codecombat.com/t/timber-guard-python/24094/6 "2020-09-14T01:38:51Z")

</div>

Whole screen, or just a screenshot of the code?

---

<div class="post-metadata">

**Author:** ![TheOrbitals](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/theorbitals/32/28144_2.png) [@TheOrbitals](https://discourse.codecombat.com/u/TheOrbitals)\
**Post date:** [September 14, 2020, 1:39am UTC](https://discourse.codecombat.com/t/timber-guard-python/24094/7 "2020-09-14T01:39:10Z")

</div>

Whole screen, also tell me what the level’s goals are  
~ Orb

---

<div class="post-metadata">

**Author:** ![CocoCharlie](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/cococharlie/32/19081_2.png) [@CocoCharlie](https://discourse.codecombat.com/u/CocoCharlie)\
**Post date:** [September 14, 2020, 1:41am UTC](https://discourse.codecombat.com/t/timber-guard-python/24094/8 "2020-09-14T01:41:47Z")

</div>

![code3](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/2X/1/1f5990bbd11c4ee37e76302290aed1f0fdd40d7e.png)  
the goals are defeat the ogres and protect the peasants.

---

<div class="post-metadata">

**Author:** ![TheOrbitals](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/theorbitals/32/28144_2.png) [@TheOrbitals](https://discourse.codecombat.com/u/TheOrbitals)\
**Post date:** [September 14, 2020, 1:47am UTC](https://discourse.codecombat.com/t/timber-guard-python/24094/9 "2020-09-14T01:47:41Z")

</div>

Oh ok I remember this now.  
So far your code is correct apart from one mistakes, but otherwise you just need to add stuff according to the comments.  
The mistake:  
`for friend in hero.findFriends():`  
`hero.findFriends()` find ALL your friends. Is there a way to only find soldier-type friends?  
`# If there's an enemy, command her to attack.`

1. You can see what tool to use in the comment (hint: look at the first word)
2. In that tool, you need to `hero.command(soldier, "attack", enemy)` to attack the enemy  
`# Otherwise, move her to the right side of the map.`
3. Look at the first word again, this tool is a sub-tool of the tool above.
4. Find a point close to that opening in the mountain there, and `hero.command(soldier, "move", {"x": thatXPos, "y": thatYPos}) Here's all the tips! ~ Orb P. S. In the next level, it is exactly the same, but you can't let your troops die or a headhunter will spawn. `# If your troop’s health \< troop’s max health / 2 (or 3), send her back to you```. This is a clue of what you should do.

---

<div class="post-metadata">

**Author:** ![Lydia\_Song](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/lydia_song/32/31461_2.png) [@Lydia\_Song](https://discourse.codecombat.com/u/Lydia_Song)\
**Post date:** [September 14, 2020, 2:03am UTC](https://discourse.codecombat.com/t/timber-guard-python/24094/10 "2020-09-14T02:03:47Z")

</div>

Can you send me the link?  
Lydia

---

<div class="post-metadata">

**Author:** ![CocoCharlie](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/cococharlie/32/19081_2.png) [@CocoCharlie](https://discourse.codecombat.com/u/CocoCharlie)\
**Post date:** [September 14, 2020, 2:04am UTC](https://discourse.codecombat.com/t/timber-guard-python/24094/11 "2020-09-14T02:04:43Z")

</div>

The link to what?(20000000chars)

---

<div class="post-metadata">

**Author:** ![Lydia\_Song](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/lydia_song/32/31461_2.png) [@Lydia\_Song](https://discourse.codecombat.com/u/Lydia_Song)\
**Post date:** [September 14, 2020, 2:05am UTC](https://discourse.codecombat.com/t/timber-guard-python/24094/12 "2020-09-14T02:05:20Z")

</div>

For Timber Guard.  
Lydia

---

<div class="post-metadata">

**Author:** ![CocoCharlie](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/cococharlie/32/19081_2.png) [@CocoCharlie](https://discourse.codecombat.com/u/CocoCharlie)\
**Post date:** [September 14, 2020, 2:14am UTC](https://discourse.codecombat.com/t/timber-guard-python/24094/13 "2020-09-14T02:14:18Z")

</div>

I don’t really know how to send a link for the level. I know where to put it in the forum, but i don’t know how to get it. I have to be done with coding today though.

---

<div class="post-metadata">

**Author:** ![TheOrbitals](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/theorbitals/32/28144_2.png) [@TheOrbitals](https://discourse.codecombat.com/u/TheOrbitals)\
**Post date:** [September 14, 2020, 2:21am UTC](https://discourse.codecombat.com/t/timber-guard-python/24094/14 "2020-09-14T02:21:14Z")

</div>

Just click the website domain link (the .com thing), and then it will pop out a longer link. Copy it all and paste it here  
~ Orb

---

<div class="post-metadata">

**Author:** ![Jake\_Doggy](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/jake_doggy/32/13887_2.png) [@Jake\_Doggy](https://discourse.codecombat.com/u/Jake_Doggy)\
**Post date:** [September 14, 2020, 4:36am UTC](https://discourse.codecombat.com/t/timber-guard-python/24094/15 "2020-09-14T04:36:02Z")

</div>

> **[CodeCombat - Coding games to learn Python and JavaScript](https://codecombat.com/play/level/timber-guard)**
>
> Learn typed code through a programming game. Learn Python, JavaScript, and HTML as you solve puzzles and learn to make your own coding games and websites.

---

<div class="post-metadata">

**Author:** ![Jake\_Doggy](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/jake_doggy/32/13887_2.png) [@Jake\_Doggy](https://discourse.codecombat.com/u/Jake_Doggy)\
**Post date:** [September 14, 2020, 4:36am UTC](https://discourse.codecombat.com/t/timber-guard-python/24094/16 "2020-09-14T04:36:44Z")

</div>

I can help you tomorrow

---

<div class="post-metadata">

**Author:** ![PeterPalov](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/peterpalov/32/45280_2.png) [@PeterPalov](https://discourse.codecombat.com/u/PeterPalov)\
**Post date:** [September 14, 2020, 4:52am UTC](https://discourse.codecombat.com/t/timber-guard-python/24094/17 "2020-09-14T04:52:54Z")

</div>

Maybe you have to write:

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

```

---

<div class="post-metadata">

**Author:** ![Lydia\_Song](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/lydia_song/32/31461_2.png) [@Lydia\_Song](https://discourse.codecombat.com/u/Lydia_Song)\
**Post date:** [September 14, 2020, 2:17pm UTC](https://discourse.codecombat.com/t/timber-guard-python/24094/18 "2020-09-14T14:17:35Z")

</div>

Thanks  
Lydia  
20 chars

---

<div class="post-metadata">

**Author:** ![Lydia\_Song](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/lydia_song/32/31461_2.png) [@Lydia\_Song](https://discourse.codecombat.com/u/Lydia_Song)\
**Post date:** [September 14, 2020, 2:21pm UTC](https://discourse.codecombat.com/t/timber-guard-python/24094/19 "2020-09-14T14:21:47Z")

</div>

So next, you just have to command your friends (AKA soldiers) to attack enemy IF there is one. And to move to one point to defend IF there is NOT an enemy.  
Hope that helps, Lydia

---

<div class="post-metadata">

**Author:** ![CocoCharlie](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/cococharlie/32/19081_2.png) [@CocoCharlie](https://discourse.codecombat.com/u/CocoCharlie)\
**Post date:** [September 15, 2020, 12:14am UTC](https://discourse.codecombat.com/t/timber-guard-python/24094/20 "2020-09-15T00:14:04Z")

</div>

how do you command them to move without a target?

[Next page](https://discourse.codecombat.com/t/timber-guard-python/24094.md?page=2)
