# \[SOLVED\] Two flowers (Python)

**URL:** <https://discourse.codecombat.com/t/solved-two-flowers-python/14403>\
**Category:** Level Help\
**Created:** [April 9, 2018, 5:35pm UTC](https://discourse.codecombat.com/t/solved-two-flowers-python/14403 "2018-04-09T17:35:22Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![123hi123](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/123hi123/32/9541_2.png) [@123hi123](https://discourse.codecombat.com/u/123hi123)\
**Post date:** [April 9, 2018, 5:35pm UTC](https://discourse.codecombat.com/t/solved-two-flowers-python/14403/1 "2018-04-09T17:35:22Z")

</div>

This 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():
    enemy = hero.findNearestEnemy()
    soldiers = hero.findByType("soldier")
    for soldier in soldiers:
        hero.command(soldier, "attack", enemy)
**# Define the function: pickUpNearestCoin**
def pickUpNearestCoin():
    item = hero.findNearestItem()
    if item:
        hero.moveXY(item.pos.x, item.pos.y)
    peasant = hero.findByType("peasant")[0]

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

```

My hero will summon soldiers, but the soldiers just protect me and Hector dies.  
Can’t figure out what is wrong

---

<div class="post-metadata">

**Author:** ![Areau](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/areau/32/7651_2.png) [@Areau](https://discourse.codecombat.com/u/Areau)\
**Post date:** [April 9, 2018, 5:37pm UTC](https://discourse.codecombat.com/t/solved-two-flowers-python/14403/2 "2018-04-09T17:37:13Z")

</div>

I am JavaScript but try telling your soldiers to find the enemy, not your hero.

```python
enemy = friend.findNearestEnemy()
or
enemy = friend.findNearest(friend.findEnemies())

```

Also maybe add an “if enemy” in there

Also quick question, what is the purpose of this line:

```python
peasant = hero.findByType(“peasant”)[0]

```

?Are you identifying Hector for some reason?

Sorry last question, I noticed you have a line separating your definition of “summonSoldiers()” but no line separating “commandSoldiers()” and “pickUpNearestCoin”. I am not familiar with Python yet but could that be a contributing factor?

Also for future reference it is good practice to post your code with the \</\> button

ie)

```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():
enemy = hero.findNearestEnemy()
soldiers = hero.findByType(“soldier”)
for soldier in soldiers:
hero.command(soldier, “attack”, enemy)
# Define the function: pickUpNearestCoin
def pickUpNearestCoin():
item = hero.findNearestItem()
if item:
hero.moveXY(item.pos.x, item.pos.y)
peasant = hero.findByType(“peasant”)[0]

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

```

---

<div class="post-metadata">

**Author:** ![xython](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/xython/32/7269_2.png) [@xython](https://discourse.codecombat.com/u/xython)\
**Post date:** [April 9, 2018, 9:50pm UTC](https://discourse.codecombat.com/t/solved-two-flowers-python/14403/3 "2018-04-09T21:50:17Z")

</div>

What kind of strange double quotes are you using? Your browser is maybe not set to English as default language. If properly indented your code must work if your double quotes are right and you

```python
hero.moveXY(item.pos.x, item.pos.y) # comment this line and replace it with
hero.move(item.pos)
#/ use the **</>** symbol above to format properly /

```

You must also replace boots if they don’t support move - moveXY has a hidden loop inside it and can add troubles when you command troops. In your case I think that they follow you hero as pets until you all die.

```python
#cmmnd can be also defend ( attack needs enemy)
#and pos can be peasant.pos or hero.pos or soldier.pos
#Try all possibilities and see what works best
hero.command(soldier, cmmnd, pos)

```

---

<div class="post-metadata">

**Author:** ![Seojin\_Roy\_Lee](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/seojin_roy_lee/32/23539_2.png) [@Seojin\_Roy\_Lee](https://discourse.codecombat.com/u/Seojin_Roy_Lee)\
**Post date:** [April 9, 2018, 11:20pm UTC](https://discourse.codecombat.com/t/solved-two-flowers-python/14403/4 "2018-04-09T23:20:32Z")

</div>

You have to see if the enemy exists. If you want it to protect Hector, then change the code to

```python
this = hero.findByType(peasant)[o]
if this:
    hero.command(friend, "defend", this)

```

---

<div class="post-metadata">

**Author:** ![123hi123](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/123hi123/32/9541_2.png) [@123hi123](https://discourse.codecombat.com/u/123hi123)\
**Post date:** [April 9, 2018, 11:31pm UTC](https://discourse.codecombat.com/t/solved-two-flowers-python/14403/5 "2018-04-09T23:31:36Z")

</div>

Thanks to you all, but I still have a problem. This is my code again

```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():
    friend = hero.findFriends()
    enemy = friend.findNearest(friend.findEnemies())
    soldiers = hero.findByType("soldier")
    for soldier in soldiers:
        hero.command(soldier, "attack", enemy)
# Define the function: pickUpNearestCoin
def pickUpNearestCoin():
    item = hero.findNearestItem()
    if item:
        hero.move(item.pos)
    this = hero.findByType("peasant")[0]
    if this:
        hero.command(friend, "defend", this)
while True:
    summonSoldiers()
    commandSoldiers()
    pickUpNearestCoin()

```

Now it says that `friend` has no method `findEnemies`

---

<div class="post-metadata">

**Author:** ![Seojin\_Roy\_Lee](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/seojin_roy_lee/32/23539_2.png) [@Seojin\_Roy\_Lee](https://discourse.codecombat.com/u/Seojin_Roy_Lee)\
**Post date:** [April 9, 2018, 11:34pm UTC](https://discourse.codecombat.com/t/solved-two-flowers-python/14403/6 "2018-04-09T23:34:45Z")

</div>

So then use `friend.findNearestEnemy()`.

---

<div class="post-metadata">

**Author:** ![123hi123](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/123hi123/32/9541_2.png) [@123hi123](https://discourse.codecombat.com/u/123hi123)\
**Post date:** [April 9, 2018, 11:39pm UTC](https://discourse.codecombat.com/t/solved-two-flowers-python/14403/7 "2018-04-09T23:39:31Z")

</div>

Changed some stuff. Here.

```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():
    friend = hero.findFriends()
    enemy = hero.findNearestEnemy()
    soldiers = hero.findByType("soldier")
    for soldier in soldiers:
        hero.command(soldier, "attack", enemy)
# Define the function: pickUpNearestCoin
def pickUpNearestCoin():
    item = hero.findNearestItem()
    if item:
        hero.move(item.pos)
    if hero.findByType("peasant"):
        hero.command(hero.findFriends, "defend", hero.findByType("peasant"))
while True:
    summonSoldiers()
    commandSoldiers()
    pickUpNearestCoin()

```

Now I get an error message: `Command`'s argument `minion` should have type unit, but got `function`. Hero Placeholder needs something to command.

---

<div class="post-metadata">

**Author:** ![Seojin\_Roy\_Lee](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/seojin_roy_lee/32/23539_2.png) [@Seojin\_Roy\_Lee](https://discourse.codecombat.com/u/Seojin_Roy_Lee)\
**Post date:** [April 10, 2018, 12:28am UTC](https://discourse.codecombat.com/t/solved-two-flowers-python/14403/8 "2018-04-10T00:28:32Z")

</div>

```python
peasants = hero.findByType("peasant")
for thing in peasants:
    if this:
        hero.command(this, "defend", thing)

```

or

```python
peasant = hero.findByType("peasant")
if peasant and this:
    hero.command(this, "defend", peasant)
    

```

---

<div class="post-metadata">

**Author:** ![123hi123](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/123hi123/32/9541_2.png) [@123hi123](https://discourse.codecombat.com/u/123hi123)\
**Post date:** [April 10, 2018, 1:43am UTC](https://discourse.codecombat.com/t/solved-two-flowers-python/14403/9 "2018-04-10T01:43:26Z")

</div>

> [@Seojin\_Roy\_Lee](#):
>
> peasants = hero.findByType(“peasant”)  
> for thing in peasants:  
> if this:  
> hero.command(this, “defend”, thing)
> 
> or
> 
> peasant = hero.findByType(“peasant”)  
> if peasant and this:  
> hero.command(this, “defend”, peasant)

where do I insert this?

---

<div class="post-metadata">

**Author:** ![Areau](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/areau/32/7651_2.png) [@Areau](https://discourse.codecombat.com/u/Areau)\
**Post date:** [April 10, 2018, 1:45am UTC](https://discourse.codecombat.com/t/solved-two-flowers-python/14403/10 "2018-04-10T01:45:02Z")

</div>

In your commandSoldiers()

---

<div class="post-metadata">

**Author:** ![123hi123](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/123hi123/32/9541_2.png) [@123hi123](https://discourse.codecombat.com/u/123hi123)\
**Post date:** [April 10, 2018, 1:57am UTC](https://discourse.codecombat.com/t/solved-two-flowers-python/14403/11 "2018-04-10T01:57:29Z")

</div>

I did what you said, but I still get an error.

```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():
    friend = hero.findFriends()
    enemy = hero.findNearestEnemy()
    soldiers = hero.findByType("soldier")
    for soldier in soldiers:
        hero.command(soldier, "attack", enemy)
    peasant = hero.findByType("peasant")
    if peasant and soldier:
        hero.command(soldier, "defend", peasant)
# Define the function: pickUpNearestCoin
def pickUpNearestCoin():
    item = hero.findNearestItem()
    if item:
        hero.move(item.pos)
while True:
    summonSoldiers()
    commandSoldiers()
    pickUpNearestCoin()

```

It says defend takes no arguments. As in the part in commandSoldiers.

---

<div class="post-metadata">

**Author:** ![123hi123](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/123hi123/32/9541_2.png) [@123hi123](https://discourse.codecombat.com/u/123hi123)\
**Post date:** [April 10, 2018, 2:07am UTC](https://discourse.codecombat.com/t/solved-two-flowers-python/14403/12 "2018-04-10T02:07:37Z")

</div>

Now I have this:

```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():
    friend = hero.findFriends()
    enemy = hero.findNearestEnemy()
    soldiers = hero.findByType("soldier")
    peasant = hero.findByType("peasant")
    x = peasant.pos.x
    y = peasant.pos.y
    for soldier in soldiers:
        hero.command(soldier, "defend", {x, y})
# Define the function: pickUpNearestCoin
def pickUpNearestCoin():
    item = hero.findNearestItem()
    if item:
        hero.move(item.pos)
while True:
    summonSoldiers()
    commandSoldiers()
    pickUpNearestCoin()

```

My hero just stands there. How do I fix that?

---

<div class="post-metadata">

**Author:** ![123hi123](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/123hi123/32/9541_2.png) [@123hi123](https://discourse.codecombat.com/u/123hi123)\
**Post date:** [April 10, 2018, 2:24am UTC](https://discourse.codecombat.com/t/solved-two-flowers-python/14403/13 "2018-04-10T02:24:59Z")

</div>

Thanks to you all!  
I got it!  
😄

---

<div class="post-metadata">

**Author:** ![Areau](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/areau/32/7651_2.png) [@Areau](https://discourse.codecombat.com/u/Areau)\
**Post date:** [April 10, 2018, 2:38am UTC](https://discourse.codecombat.com/t/solved-two-flowers-python/14403/14 "2018-04-10T02:38:31Z")

</div>

Please don’t post solutions on the board, thanks 123hi123!

It took me a little bit because I don’t know python but I got it as well lol (not in time to help you though!) I didn’t try defend because I am not familiar with the language but I am happy you got it!

If you want to post spoilers people usually put them between a

```python
[spoiler] text here [/spoiler]

```

Like this! \<- To get this =) (click to reveal)

Also can you add [SOLVED] to your topic header?

---

<div class="post-metadata">

**Author:** ![zython](https://avatars.discourse-cdn.com/v4/letter/z/65b543/32.png) [@zython](https://discourse.codecombat.com/u/zython)\
**Post date:** [July 14, 2020, 3:42am UTC](https://discourse.codecombat.com/t/solved-two-flowers-python/14403/15 "2020-07-14T03:42:32Z")

</div>

how can I solve it?  
P.S: I nearly spoils

```python
def summonSoldiers():
    if hero.gold >= hero.costOf("soldier"):
        hero.summon("soldier")
def commandSoldiers():
    friend = hero.findFriends()
    soldiers = hero.findByType("soldier")
    peasant = hero.findByType("peasant")
    for soldier in soldiers:
        hero.command(soldier, "defend", {'x':peasant.pos.x, 'y': peasant.pos.y})
def pickUpNearestCoin():
    item = hero.findNearestItem()
    if item:
        hero.move(item.pos)
while True:
    summonSoldiers()
    commandSoldiers()
    pickUpNearestCoin()

```

zython

---

<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:** [July 14, 2020, 3:49am UTC](https://discourse.codecombat.com/t/solved-two-flowers-python/14403/16 "2020-07-14T03:49:46Z")

</div>

> [@zython](#):
>
> `peasant = hero.findByType("peasant")`

Here you should select only an element of the array that findByType gives you, not the whole array (maybe something like hero.findByType(“peasant”)[0]?)

> [@zython](#):
>
> `hero.command(soldier, "defend", {'x':peasant.pos.x, 'y': peasant.pos.y})`

Here you should command your soldiers to attack their nearest enemy instead of defending the peasant’s position.

Andrei

---

<div class="post-metadata">

**Author:** ![zython](https://avatars.discourse-cdn.com/v4/letter/z/65b543/32.png) [@zython](https://discourse.codecombat.com/u/zython)\
**Post date:** [July 14, 2020, 4:03am UTC](https://discourse.codecombat.com/t/solved-two-flowers-python/14403/17 "2020-07-14T04:03:20Z")

</div>

it works at the start, but, all the soldiers dies at the end and Heator died too.☹

---

<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:** [July 14, 2020, 4:03am UTC](https://discourse.codecombat.com/t/solved-two-flowers-python/14403/18 "2020-07-14T04:03:57Z")

</div>

Can you send me your new code?

Andrei

---

<div class="post-metadata">

**Author:** ![zython](https://avatars.discourse-cdn.com/v4/letter/z/65b543/32.png) [@zython](https://discourse.codecombat.com/u/zython)\
**Post date:** [July 14, 2020, 4:04am UTC](https://discourse.codecombat.com/t/solved-two-flowers-python/14403/19 "2020-07-14T04:04:31Z")

</div>

pass!

[en\_US.composer.my\_button\_text]

---

<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:** [July 14, 2020, 4:04am UTC](https://discourse.codecombat.com/t/solved-two-flowers-python/14403/20 "2020-07-14T04:04:56Z")

</div>

Congratulations for completing the level then! 🥳

Andrei

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