# \[SOLVED\]Two Flowers

**URL:** <https://discourse.codecombat.com/t/solved-two-flowers/27086>\
**Category:** Level Help\
**Created:** [January 28, 2021, 6:10pm UTC](https://discourse.codecombat.com/t/solved-two-flowers/27086 "2021-01-28T18:10:19Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ryan\_Wong](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/ryan_wong/32/25710_2.png) [@Ryan\_Wong](https://discourse.codecombat.com/u/Ryan_Wong)\
**Post date:** [January 28, 2021, 6:10pm UTC](https://discourse.codecombat.com/t/solved-two-flowers/27086/1 "2021-01-28T18:10:20Z")

</div>

I can’t figure it out its so frustrating!!!  
here is my code:

```python
# If the peasant is damaged, the flowers will shrink!

def summonSoldiers():
    if hero.gold >= hero.costOf("soldier"):
        hero.summon("soldier")

# Define the function: commandSoldiers
def commandSoldiers():
    friends = hero.findFriends()
    for friend in friends:
        enemy = friend.findNearestEnemy()
        soldiers = hero.findByType("soldier")
        for soldier in soldiers:
            if enemy:
                hero.command(soldier, "attack", enemy)

# Define the function: pickUpNearestCoin
def pickUpNearestCoin():
    item = hero.findNearestItem()
    if item:
        hero.move(item.pos)

peasant = hero.findByType("peasant")[0]

while True:
    summonSoldiers()
    # commandSoldiers()
    commandSoldiers()
    # pickUpNearestCoin()
    pickUpNearestCoin()

```

please help

---

<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:** [January 28, 2021, 6:15pm UTC](https://discourse.codecombat.com/t/solved-two-flowers/27086/2 "2021-01-28T18:15:08Z")

</div>

Try to do this

```python
friends = hero.findByType("peasant", hero.findFriends())
  for friends in friends:
       enemy = friend.findNearestEnemy()
      if enemy:
         hero.command(friend, "attack" enemy)
        

```

This way it attacks the closest enemy to the peasant

---

<div class="post-metadata">

**Author:** ![Ryan\_Wong](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/ryan_wong/32/25710_2.png) [@Ryan\_Wong](https://discourse.codecombat.com/u/Ryan_Wong)\
**Post date:** [January 28, 2021, 6:15pm UTC](https://discourse.codecombat.com/t/solved-two-flowers/27086/3 "2021-01-28T18:15:39Z")

</div>

so where should I put the code?

---

<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:** [January 28, 2021, 6:16pm UTC](https://discourse.codecombat.com/t/solved-two-flowers/27086/4 "2021-01-28T18:16:31Z")

</div>

do it in the command friends thing

---

<div class="post-metadata">

**Author:** ![Ryan\_Wong](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/ryan_wong/32/25710_2.png) [@Ryan\_Wong](https://discourse.codecombat.com/u/Ryan_Wong)\
**Post date:** [January 28, 2021, 6:19pm UTC](https://discourse.codecombat.com/t/solved-two-flowers/27086/5 "2021-01-28T18:19:13Z")

</div>

huh I don’t know where is the command friends thing

---

<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:** [January 28, 2021, 6:20pm UTC](https://discourse.codecombat.com/t/solved-two-flowers/27086/6 "2021-01-28T18:20:04Z")

</div>

Do it for command soldiers.

---

<div class="post-metadata">

**Author:** ![Ryan\_Wong](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/ryan_wong/32/25710_2.png) [@Ryan\_Wong](https://discourse.codecombat.com/u/Ryan_Wong)\
**Post date:** [January 28, 2021, 6:21pm UTC](https://discourse.codecombat.com/t/solved-two-flowers/27086/7 "2021-01-28T18:21:37Z")

</div>

it still doesn’t work

---

<div class="post-metadata">

**Author:** ![Ryan\_Wong](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/ryan_wong/32/25710_2.png) [@Ryan\_Wong](https://discourse.codecombat.com/u/Ryan_Wong)\
**Post date:** [January 28, 2021, 6:21pm UTC](https://discourse.codecombat.com/t/solved-two-flowers/27086/8 "2021-01-28T18:21:58Z")

</div>

here is my entire code:

```python
# If the peasant is damaged, the flowers will shrink!

def summonSoldiers():
    if hero.gold >= hero.costOf("soldier"):
        hero.summon("soldier")

# Define the function: commandSoldiers
def commandSoldiers():
    friends = hero.findFriends()
    for friend in friends:
        enemy = friend.findNearestEnemy()
        soldiers = hero.findByType("soldier")
        for soldier in soldiers:
            if enemy:
                hero.command(soldier, "attack", enemy)
                friends = hero.findByType("peasant", hero.findFriends())
    for friends in friends:
        enemy = friend.findNearestEnemy()
if enemy:
    hero.command(friend, "attack", enemy)

# Define the function: pickUpNearestCoin
def pickUpNearestCoin():
    item = hero.findNearestItem()
    if item:
        hero.move(item.pos)

peasant = hero.findByType("peasant")[0]

while True:
    summonSoldiers()
    # commandSoldiers()
    commandSoldiers()
    # pickUpNearestCoin()
    pickUpNearestCoin()

```

---

<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:** [January 28, 2021, 6:24pm UTC](https://discourse.codecombat.com/t/solved-two-flowers/27086/9 "2021-01-28T18:24:34Z")

</div>

Get rid of this

> [@Ryan\_Wong](#):
>
> ```python
> friends = hero.findFriends()
> for friend in friends:
> enemy = friend.findNearestEnemy()
> soldiers = hero.findByType("soldier")
> for soldier in soldiers:
> if enemy:
> hero.command(soldier, "attack", enemy)
> 
> ```

And indent this properly

> [@Ryan\_Wong](#):
>
> ```python
> friends = hero.findByType("peasant", hero.findFriends())
> for friends in friends:
> enemy = friend.findNearestEnemy()
> if enemy:
> hero.command(friend, "attack", enemy)
> 
> ```

---

<div class="post-metadata">

**Author:** ![Ryan\_Wong](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/ryan_wong/32/25710_2.png) [@Ryan\_Wong](https://discourse.codecombat.com/u/Ryan_Wong)\
**Post date:** [January 28, 2021, 6:27pm UTC](https://discourse.codecombat.com/t/solved-two-flowers/27086/10 "2021-01-28T18:27:49Z")

</div>

it still doesn’t work

---

<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:** [January 28, 2021, 6:28pm UTC](https://discourse.codecombat.com/t/solved-two-flowers/27086/11 "2021-01-28T18:28:08Z")

</div>

show me your new code now and what happens

---

<div class="post-metadata">

**Author:** ![Ryan\_Wong](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/ryan_wong/32/25710_2.png) [@Ryan\_Wong](https://discourse.codecombat.com/u/Ryan_Wong)\
**Post date:** [January 28, 2021, 6:28pm UTC](https://discourse.codecombat.com/t/solved-two-flowers/27086/12 "2021-01-28T18:28:26Z")

</div>

here is my new code:

```python
# If the peasant is damaged, the flowers will shrink!

def summonSoldiers():
    if hero.gold >= hero.costOf("soldier"):
        hero.summon("soldier")

# Define the function: commandSoldiers
def commandSoldiers():
    friends = hero.findByType("peasant", hero.findFriends())
    for friends in friends:
        enemy = hero.findNearestEnemy()
if enemy:
    hero.command(friend, "attack", enemy)

# Define the function: pickUpNearestCoin
friends = hero.findByType("peasant", hero.findFriends())
for friends in friends:
    enemy = friend.findNearestEnemy()
if enemy:
    hero.command(friend, "attack", enemy)
peasant = hero.findByType("peasant")[0]

while True:
    summonSoldiers()
    # commandSoldiers()
    commandSoldiers()
    # pickUpNearestCoin()
    pickUpNearestCoin()

```

---

<div class="post-metadata">

**Author:** ![Ryan\_Wong](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/ryan_wong/32/25710_2.png) [@Ryan\_Wong](https://discourse.codecombat.com/u/Ryan_Wong)\
**Post date:** [January 28, 2021, 6:29pm UTC](https://discourse.codecombat.com/t/solved-two-flowers/27086/13 "2021-01-28T18:29:41Z")

</div>

and it say’s try `hero.findFriends`

---

<div class="post-metadata">

**Author:** ![JoPro\_8000](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/jopro_8000/32/27121_2.png) [@JoPro\_8000](https://discourse.codecombat.com/u/JoPro_8000)\
**Post date:** [January 28, 2021, 6:46pm UTC](https://discourse.codecombat.com/t/solved-two-flowers/27086/14 "2021-01-28T18:46:00Z")

</div>

Ill help you soon, just let me start up CoCo ok?

---

<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:** [January 28, 2021, 6:46pm UTC](https://discourse.codecombat.com/t/solved-two-flowers/27086/15 "2021-01-28T18:46:23Z")

</div>

Hello and welcome to codecombat discourse! This is a cozy forum where you can share ideas, share fan art, get assistance for code, etc! Before you proceed, we hope that you review [this topic](https://discourse.codecombat.com/t/welcome-to-codecombat-discourse-forums/15601), which shows all essentials of this board! Thanks!

---

<div class="post-metadata">

**Author:** ![JoPro\_8000](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/jopro_8000/32/27121_2.png) [@JoPro\_8000](https://discourse.codecombat.com/u/JoPro_8000)\
**Post date:** [January 28, 2021, 6:49pm UTC](https://discourse.codecombat.com/t/solved-two-flowers/27086/16 "2021-01-28T18:49:54Z")

</div>

Thanks for welcoming me! I’ve been here before tho 🙂

---

<div class="post-metadata">

**Author:** ![Ryan\_Wong](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/ryan_wong/32/25710_2.png) [@Ryan\_Wong](https://discourse.codecombat.com/u/Ryan_Wong)\
**Post date:** [January 28, 2021, 6:53pm UTC](https://discourse.codecombat.com/t/solved-two-flowers/27086/18 "2021-01-28T18:53:43Z")

</div>

ok then what do I do?

---

<div class="post-metadata">

**Author:** ![Ryan\_Wong](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/ryan_wong/32/25710_2.png) [@Ryan\_Wong](https://discourse.codecombat.com/u/Ryan_Wong)\
**Post date:** [January 28, 2021, 6:58pm UTC](https://discourse.codecombat.com/t/solved-two-flowers/27086/19 "2021-01-28T18:58:24Z")

</div>

um how do I do that step?

---

<div class="post-metadata">

**Author:** ![JoPro\_8000](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/jopro_8000/32/27121_2.png) [@JoPro\_8000](https://discourse.codecombat.com/u/JoPro_8000)\
**Post date:** [January 28, 2021, 7:01pm UTC](https://discourse.codecombat.com/t/solved-two-flowers/27086/20 "2021-01-28T19:01:48Z")

</div>

delete the command Soldiers funtion and instead put this:

```python
def commandSoldiers():
    for soldier in hero.findFriends():
        enemy = soldier.findNearestEnemy()
        if enemy and soldier.type == "soldier":
            hero.command(soldier, "attack", enemy)

```

---

<div class="post-metadata">

**Author:** ![Ryan\_Wong](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/ryan_wong/32/25710_2.png) [@Ryan\_Wong](https://discourse.codecombat.com/u/Ryan_Wong)\
**Post date:** [January 28, 2021, 7:02pm UTC](https://discourse.codecombat.com/t/solved-two-flowers/27086/21 "2021-01-28T19:02:58Z")

</div>

ok then what do I do?

[Next page](https://discourse.codecombat.com/t/solved-two-flowers/27086.md?page=2)
