# \[SOLVED\] Timber Guard help please

**URL:** <https://discourse.codecombat.com/t/solved-timber-guard-help-please/22520>\
**Category:** Level Help\
**Created:** [June 24, 2020, 1:57pm UTC](https://discourse.codecombat.com/t/solved-timber-guard-help-please/22520 "2020-06-24T13:57:03Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![098765432123](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/098765432123/32/25850_2.png) [@098765432123](https://discourse.codecombat.com/u/098765432123)\
**Post date:** [June 24, 2020, 1:57pm UTC](https://discourse.codecombat.com/t/solved-timber-guard-help-please/22520/1 "2020-06-24T13:57:03Z")

</div>

im having issues with my code. help is really appreciated

---

<div class="post-metadata">

**Author:** ![098765432123](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/098765432123/32/25850_2.png) [@098765432123](https://discourse.codecombat.com/u/098765432123)\
**Post date:** [June 24, 2020, 1:57pm UTC](https://discourse.codecombat.com/t/solved-timber-guard-help-please/22520/2 "2020-06-24T13:57:16Z")

</div>

```python
if gold:
    hero.moveXY(gold.pos.x, gold.pos.y)
# If you have enough gold, summon a soldier.
if {hero.gold > hero.costOf("soldier")}:
    hero.summon("soldier")
# 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()
        defendPos = {"x": 91, "y": 40}
        # If there's an enemy, command her to attack.
        # Otherwise, move her to the right side of the map.
        if enemy:
            hero.command(friend, "attack", enemy)
        else:
            hero.command(friend, "move", defendPos)

```

---

<div class="post-metadata">

**Author:** ![098765432123](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/098765432123/32/25850_2.png) [@098765432123](https://discourse.codecombat.com/u/098765432123)\
**Post date:** [June 24, 2020, 1:57pm UTC](https://discourse.codecombat.com/t/solved-timber-guard-help-please/22520/3 "2020-06-24T13:57:56Z")

</div>

@AnSeDra (20characters)

---

<div class="post-metadata">

**Author:** ![AnSeDra](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/ansedra/32/13123_2.png) [@AnSeDra](https://discourse.codecombat.com/u/AnSeDra)\
**Post date:** [June 24, 2020, 2:00pm UTC](https://discourse.codecombat.com/t/solved-timber-guard-help-please/22520/4 "2020-06-24T14:00:51Z")

</div>

> [@098765432123](#):
>
> ```python
> if gold:
> hero.moveXY(gold.pos.x, gold.pos.y)
> # If you have enough gold, summon a soldier.
> if {hero.gold > hero.costOf("soldier")}:
> hero.summon("soldier")
> # 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()
> defendPos = {"x": 91, "y": 40}
> # If there's an enemy, command her to attack.
> # Otherwise, move her to the right side of the map.
> if enemy:
> hero.command(friend, "attack", enemy)
> else:
> hero.command(friend, "move", defendPos)
> 
> ```

Try to put all of this in a while True and before the if gold put a:

```python
gold=hero.findNearestItem()

```

---

<div class="post-metadata">

**Author:** ![098765432123](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/098765432123/32/25850_2.png) [@098765432123](https://discourse.codecombat.com/u/098765432123)\
**Post date:** [June 24, 2020, 2:03pm UTC](https://discourse.codecombat.com/t/solved-timber-guard-help-please/22520/5 "2020-06-24T14:03:55Z")

</div>

my hero dosen’t move and a error screen appears saying “don’t know how to transform: set”

---

<div class="post-metadata">

**Author:** ![AnSeDra](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/ansedra/32/13123_2.png) [@AnSeDra](https://discourse.codecombat.com/u/AnSeDra)\
**Post date:** [June 24, 2020, 2:04pm UTC](https://discourse.codecombat.com/t/solved-timber-guard-help-please/22520/6 "2020-06-24T14:04:39Z")

</div>

Can you show me your code?

---

<div class="post-metadata">

**Author:** ![098765432123](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/098765432123/32/25850_2.png) [@098765432123](https://discourse.codecombat.com/u/098765432123)\
**Post date:** [June 24, 2020, 2:04pm UTC](https://discourse.codecombat.com/t/solved-timber-guard-help-please/22520/7 "2020-06-24T14:04:54Z")

</div>

```python
while True:
    gold=hero.findNearestItem()
    # If you have enough gold, summon a soldier.
    if {hero.gold > hero.costOf("soldier")}:
        hero.summon("soldier")
        # 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 the current item.
        for friend in hero.findFriends():
            if friend.type == "soldier":
                enemy = friend.findNearestEnemy()
                defendPos = {"x": 91, "y": 40}
                # If there's an enemy, command her to attack.
                # Otherwise, move her to the right side of the map.
                if enemy:
                    hero.command(friend, "attack", enemy)
                else:
                    hero.command(friend, "move", defendPos)

```

---

<div class="post-metadata">

**Author:** ![AnSeDra](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/ansedra/32/13123_2.png) [@AnSeDra](https://discourse.codecombat.com/u/AnSeDra)\
**Post date:** [June 24, 2020, 2:05pm UTC](https://discourse.codecombat.com/t/solved-timber-guard-help-please/22520/8 "2020-06-24T14:05:46Z")

</div>

Don’t forget to put this

> [@098765432123](#):
>
> ```python
> if gold:
> hero.moveXY(gold.pos.x, gold.pos.y)
> 
> ```

After this

> [@098765432123](#):
>
> `gold=hero.findNearestItem()`

---

<div class="post-metadata">

**Author:** ![AnSeDra](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/ansedra/32/13123_2.png) [@AnSeDra](https://discourse.codecombat.com/u/AnSeDra)\
**Post date:** [June 24, 2020, 2:06pm UTC](https://discourse.codecombat.com/t/solved-timber-guard-help-please/22520/9 "2020-06-24T14:06:17Z")

</div>

> [@098765432123](#):
>
> `if {hero.gold > hero.costOf("soldier")}:`

Try to delete the **{** and the **}**

---

<div class="post-metadata">

**Author:** ![AnSeDra](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/ansedra/32/13123_2.png) [@AnSeDra](https://discourse.codecombat.com/u/AnSeDra)\
**Post date:** [June 24, 2020, 2:07pm UTC](https://discourse.codecombat.com/t/solved-timber-guard-help-please/22520/10 "2020-06-24T14:07:09Z")

</div>

> [@098765432123](#):
>
> ```python
> for friend in hero.findFriends():
> if friend.type == "soldier":
> enemy = friend.findNearestEnemy()
> defendPos = {"x": 91, "y": 40}
> # If there's an enemy, command her to attack.
> # Otherwise, move her to the right side of the map.
> if enemy:
> hero.command(friend, "attack", enemy)
> else:
> hero.command(friend, "move", defendPos)
> 
> ```

Try to put this outside this if

> [@098765432123](#):
>
> `if {hero.gold > hero.costOf("soldier")}:`

Do you need any more assistance at this level?

---

<div class="post-metadata">

**Author:** ![098765432123](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/098765432123/32/25850_2.png) [@098765432123](https://discourse.codecombat.com/u/098765432123)\
**Post date:** [June 24, 2020, 2:08pm UTC](https://discourse.codecombat.com/t/solved-timber-guard-help-please/22520/11 "2020-06-24T14:08:33Z")

</div>

yes my hero now collects gold and spawns troops but the troops dont attack the enemys they just go to the far right side of the screen

---

<div class="post-metadata">

**Author:** ![098765432123](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/098765432123/32/25850_2.png) [@098765432123](https://discourse.codecombat.com/u/098765432123)\
**Post date:** [June 24, 2020, 2:08pm UTC](https://discourse.codecombat.com/t/solved-timber-guard-help-please/22520/12 "2020-06-24T14:08:56Z")

</div>

```python
while True:
    gold=hero.findNearestItem()
    if gold:
        hero.moveXY(gold.pos.x, gold.pos.y)
    # If you have enough gold, summon a soldier.
    if hero.gold > hero.costOf("soldier"):
        hero.summon("soldier")
        # 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 the current item.
        for friend in hero.findFriends():
            if friend.type == "soldier":
                enemy = friend.findNearestEnemy()
                defendPos = {"x": 91, "y": 40}
                # If there's an enemy, command her to attack.
                # Otherwise, move her to the right side of the map.
                if enemy:
                    hero.command(friend, "attack", enemy)
                else:
                        hero.command(friend, "move", defendPos)

```

---

<div class="post-metadata">

**Author:** ![AnSeDra](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/ansedra/32/13123_2.png) [@AnSeDra](https://discourse.codecombat.com/u/AnSeDra)\
**Post date:** [June 24, 2020, 2:09pm UTC](https://discourse.codecombat.com/t/solved-timber-guard-help-please/22520/13 "2020-06-24T14:09:39Z")

</div>

> [@098765432123](#):
>
> ```python
> for friend in hero.findFriends():
> if friend.type == "soldier":
> enemy = friend.findNearestEnemy()
> defendPos = {"x": 91, "y": 40}
> # If there's an enemy, command her to attack.
> # Otherwise, move her to the right side of the map.
> if enemy:
> hero.command(friend, "attack", enemy)
> else:
> hero.command(friend, "move", defendPos)
> 
> ```

Put this outside this if

> [@098765432123](#):
>
> ```python
> if hero.gold > hero.costOf("soldier"):
> hero.summon("soldier")
> 
> ```

Do you need any more assistance at this level?

---

<div class="post-metadata">

**Author:** ![098765432123](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/098765432123/32/25850_2.png) [@098765432123](https://discourse.codecombat.com/u/098765432123)\
**Post date:** [June 24, 2020, 2:11pm UTC](https://discourse.codecombat.com/t/solved-timber-guard-help-please/22520/14 "2020-06-24T14:11:00Z")

</div>

the troops are attacking but the enemy gets there to quick

---

<div class="post-metadata">

**Author:** ![AnSeDra](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/ansedra/32/13123_2.png) [@AnSeDra](https://discourse.codecombat.com/u/AnSeDra)\
**Post date:** [June 24, 2020, 2:11pm UTC](https://discourse.codecombat.com/t/solved-timber-guard-help-please/22520/15 "2020-06-24T14:11:26Z")

</div>

Can you show me your code again?

---

<div class="post-metadata">

**Author:** ![098765432123](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/098765432123/32/25850_2.png) [@098765432123](https://discourse.codecombat.com/u/098765432123)\
**Post date:** [June 24, 2020, 2:11pm UTC](https://discourse.codecombat.com/t/solved-timber-guard-help-please/22520/16 "2020-06-24T14:11:47Z")

</div>

```python
while True:
    gold=hero.findNearestItem()
    if gold:
        hero.moveXY(gold.pos.x, gold.pos.y)
    # If you have enough gold, summon a soldier.
    if hero.gold > hero.costOf("soldier"):
        hero.summon("soldier")
        # 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 the current item.
    for friend in hero.findFriends():
        if friend.type == "soldier":
            enemy = friend.findNearestEnemy()
            defendPos = {"x": 91, "y": 40}
            # If there's an enemy, command her to attack.
            # Otherwise, move her to the right side of the map.
            if enemy:
                hero.command(friend, "attack", enemy)
            else:
                hero.command(friend, "move", defendPos)

```

---

<div class="post-metadata">

**Author:** ![AnSeDra](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/ansedra/32/13123_2.png) [@AnSeDra](https://discourse.codecombat.com/u/AnSeDra)\
**Post date:** [June 24, 2020, 2:12pm UTC](https://discourse.codecombat.com/t/solved-timber-guard-help-please/22520/17 "2020-06-24T14:12:30Z")

</div>

> [@098765432123](#):
>
> `defendPos = {"x": 91, "y": 40}`

Try to chance the x and y coordonates of the defendPos.

---

<div class="post-metadata">

**Author:** ![098765432123](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/098765432123/32/25850_2.png) [@098765432123](https://discourse.codecombat.com/u/098765432123)\
**Post date:** [June 24, 2020, 2:13pm UTC](https://discourse.codecombat.com/t/solved-timber-guard-help-please/22520/18 "2020-06-24T14:13:38Z")

</div>

what do you want me to change the pos to? and do you need a sword for this lvl or no?

---

<div class="post-metadata">

**Author:** ![AnSeDra](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/ansedra/32/13123_2.png) [@AnSeDra](https://discourse.codecombat.com/u/AnSeDra)\
**Post date:** [June 24, 2020, 2:14pm UTC](https://discourse.codecombat.com/t/solved-timber-guard-help-please/22520/19 "2020-06-24T14:14:38Z")

</div>

You do not need a sword. As for coordonates, try to improvise until you get it right.  
Can you let me know when have you finished the level?

---

<div class="post-metadata">

**Author:** ![098765432123](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/098765432123/32/25850_2.png) [@098765432123](https://discourse.codecombat.com/u/098765432123)\
**Post date:** [June 24, 2020, 2:16pm UTC](https://discourse.codecombat.com/t/solved-timber-guard-help-please/22520/20 "2020-06-24T14:16:46Z")

</div>

it finally working! thanks @AnSeDra for the help! 😃

[Next page](https://discourse.codecombat.com/t/solved-timber-guard-help-please/22520.md?page=2)
