Two flowers problem, shouldn't it win by itself?

Hello everybody!

As I understand this level, just fulfilling the functions as in the previous level it should win. I tried but a lot of ogres appear and my soldiers can not beat them. I tried to add a function attacking the enemies if they are more than 10 but even like that too many appear. Does it mean that I need to improve my equipment? or am I doing something wrong?

Here my code:


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

# Define the function: commandSoldiers
def commandSoldiers():
    peasant = self.findByType("peasant")[0]
    friends=self.findFriends()
    
    for friend in friends:
        if friend.type != 'peasant':
            enemies= peasant.findEnemies()
            if enemies:
                for enemy in enemies:
                    self.command(friend, "attack", enemy)
# Define the function: pickUpNearestCoin
def pickUpNearestCoin():
    coin=self.findNearest(self.findItems())
    self.moveXY(coin.pos.x, coin.pos.y)
peasant = self.findByType("peasant")[0]
def attackmalos():
    malos=self.findEnemies()
    if len(malos)>10:
        for malo in malos:
            if self.isReady("bash"):
                self.bash(malo)
            else if self.canElectrocute(malo):
                self.electrocute(malo)
            else:
                self.attack(malo)
loop:
    summonSoldiers()
    commandSoldiers()
    pickUpNearestCoin()
    attackmalos()

Thanks in advance

A better idea would just be to command the soldiers to attack their nearest enemy, instead of moving from enemy to enemy.

You are right about go from enemy to enemy, but if the soldiers attack their nearest some ogres kill Hector, now I tried this in order to protect more Hector but i get tmp146 is undefined:

    for friend in friends:
        if friend.type != 'peasant':
            enemy= peasant.findNearest(peasant.findEnemies())
            if enemy:
                self.command(friend, "attack", enemy)