# The Two Flowers - Python Help

**URL:** <https://discourse.codecombat.com/t/the-two-flowers-python-help/11060>\
**Category:** Uncategorized\
**Created:** [March 31, 2017, 8:13pm UTC](https://discourse.codecombat.com/t/the-two-flowers-python-help/11060 "2017-03-31T20:13:38Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![B\_Hok](https://avatars.discourse-cdn.com/v4/letter/b/5daacb/32.png) [@B\_Hok](https://discourse.codecombat.com/u/B_Hok)\
**Post date:** [March 31, 2017, 8:13pm UTC](https://discourse.codecombat.com/t/the-two-flowers-python-help/11060/1 "2017-03-31T20:13:38Z")

</div>

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

def commandSoldiers():
soldiers = hero.findFriends()
for soldier in hero.findFriends():
    enemy = soldier.findNearestEnemy()
    if enemy:
        hero.command(soldier, "attack", enemy)

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

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

```

I keep getting an error that command’s argument minion should have type unit, but got object: hector

---

<div class="post-metadata">

**Author:** ![Luke10](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/luke10/32/19892_2.png) [@Luke10](https://discourse.codecombat.com/u/Luke10)\
**Post date:** [April 2, 2017, 4:50pm UTC](https://discourse.codecombat.com/t/the-two-flowers-python-help/11060/2 "2017-04-02T16:50:56Z")

</div>

> [@B\_Hok](#):
>
> enemy = soldier.findNearestEnemy()  
> if enemy:  
> hero.command(soldier, “attack”, enemy)

I think you need `self.findNearestEnemy`

---

<div class="post-metadata">

**Author:** ![Adam\_King](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/adam_king/32/2705_2.png) [@Adam\_King](https://discourse.codecombat.com/u/Adam_King)\
**Post date:** [January 12, 2018, 8:33pm UTC](https://discourse.codecombat.com/t/the-two-flowers-python-help/11060/3 "2018-01-12T20:33:16Z")

</div>

```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():
    soldiers = hero.findByType("soldier")
    for i in soldiers[i]:
        hero.command(soldiers[i], "attack", friend.findNearestEnemy())
# Define the function: pickUpNearestCoin
def pickUpNearestCoin():
    coin = hero.findNearestItem()
    if coin:
        hero.moveXY(coin.pos.x, coin.pos.y)

while True:
    peasant = hero.findByType("peasant")[0]
    summonSoldiers()
    commandSoldiers()
    pickUpNearestCoin()

```

Whenever I press run, Tharin has the red X and says that it cannot convert undefined/null to object.

 ![image](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/2X/8/8def89dcd2e8b3ce59e80f5b593853133b353b7b.png)

---

<div class="post-metadata">

**Author:** ![kyay10](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/kyay10/32/7337_2.png) [@kyay10](https://discourse.codecombat.com/u/kyay10)\
**Post date:** [January 13, 2018, 2:40am UTC](https://discourse.codecombat.com/t/the-two-flowers-python-help/11060/4 "2018-01-13T02:40:46Z")

</div>

> [@Adam\_King](#):
>
> ```python
> def commandSoldiers():
> soldiers = hero.findByType("soldier")
> for i in soldiers[i]:
> hero.command(soldiers[i], "attack", friend.findNearestEnemy())
> 
> ```

it should be `for i in range(0,len(soldiers)-1)` or something like this  
and it should be soldiers[i].findNearestEnemy()

---

<div class="post-metadata">

**Author:** ![Adam\_King](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/adam_king/32/2705_2.png) [@Adam\_King](https://discourse.codecombat.com/u/Adam_King)\
**Post date:** [January 13, 2018, 6:27pm UTC](https://discourse.codecombat.com/t/the-two-flowers-python-help/11060/5 "2018-01-13T18:27:20Z")

</div>

How they showed me and displayed for loops was `for i in soldiers[i]:`

---

<div class="post-metadata">

**Author:** ![kyay10](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/kyay10/32/7337_2.png) [@kyay10](https://discourse.codecombat.com/u/kyay10)\
**Post date:** [January 13, 2018, 7:02pm UTC](https://discourse.codecombat.com/t/the-two-flowers-python-help/11060/6 "2018-01-13T19:02:08Z")

</div>

ok so change friend.findNearestEnemy() to soldier.findNearestEnemy()

---

<div class="post-metadata">

**Author:** ![Timothy\_Johnson](https://avatars.discourse-cdn.com/v4/letter/t/9f8e36/32.png) [@Timothy\_Johnson](https://discourse.codecombat.com/u/Timothy_Johnson)\
**Post date:** [March 26, 2018, 6:33pm UTC](https://discourse.codecombat.com/t/the-two-flowers-python-help/11060/7 "2018-03-26T18:33:09Z")

</div>

I need help with my code, he summons soldiers and stuff but then he doesn’t do anything else after that,he just stands there, which I expected but he wont command them to attack enemies!

```python
    coin = self.findNearest(self.findItems())
    self.move(coin.pos)
    if hero.time == 10:
        break
loop:
    
    if self.gold > self.costOf("soldier"):
        self.summon("soldier")
        
    enemy = self.findNearest(self.findEnemies()) 
    if enemy:
        # Loop over all your soldiers and order them to attack.
        soldiers = self.findFriends()
        if hero.findFriends() != "hector":
            continue
        else:
            pass
        soldierIndex = 0
        
        while soldierIndex < len(soldiers):
            soldier = soldiers[soldierIndex]
            enemy = soldier.findNearest(enemy)
            self.command(soldier, "attack", enemy)
            soldierIndex = soldierIndex+1
```

---

<div class="post-metadata">

**Author:** ![Gabriel\_Dupuis](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/gabriel_dupuis/32/7450_2.png) [@Gabriel\_Dupuis](https://discourse.codecombat.com/u/Gabriel_Dupuis)\
**Post date:** [March 26, 2018, 6:58pm UTC](https://discourse.codecombat.com/t/the-two-flowers-python-help/11060/8 "2018-03-26T18:58:02Z")

</div>

You are asking the soldiers to look in an array, you are not using the right function.

```python
while soldierIndex < len(soldiers):
            soldier = soldiers[soldierIndex]
            enemy = soldier.findNearest(enemy) <----- # this won't work
            self.command(soldier, "attack", enemy)
            soldierIndex = soldierIndex+1

```

---

<div class="post-metadata">

**Author:** ![Timothy\_Johnson](https://avatars.discourse-cdn.com/v4/letter/t/9f8e36/32.png) [@Timothy\_Johnson](https://discourse.codecombat.com/u/Timothy_Johnson)\
**Post date:** [March 28, 2018, 6:41pm UTC](https://discourse.codecombat.com/t/the-two-flowers-python-help/11060/9 "2018-03-28T18:41:16Z")

</div>

so what do i do with that part then? i did happen to beat the level by now, but in a different way, the wrong way but i beat it, however it would be good to know how to do that.

---

<div class="post-metadata">

**Author:** ![Gabriel\_Dupuis](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/gabriel_dupuis/32/7450_2.png) [@Gabriel\_Dupuis](https://discourse.codecombat.com/u/Gabriel_Dupuis)\
**Post date:** [March 28, 2018, 7:09pm UTC](https://discourse.codecombat.com/t/the-two-flowers-python-help/11060/10 "2018-03-28T19:09:31Z")

</div>

ill explain in detail

when you say enemy = **soldier.findNearest(enemy)**  
you are asking the soldier to look in the list or array: enemy,  
which doesn’t exist

when you say enemy = **soldier.findNearestEnemy()**  
you are asking the soldier to find the nearest enemy.

what is a list?  
It is a changeable data containing multiple variables that can be anything.  
list = [value1, value2, value3, value4]

You’ll learn about arrays/list in cloudrip mountains campaign
