[SOLVED] Two Flowers Python

I tried reading other topics already, they didn’t help
I’m getting the “code is really slow or never finished” warning

My code:

# 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:
            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()

Help? Someone? Please?

The problem is in your commandSoldiers() function. You need an if enemy in there. Also, you don’t need to define soldier, but you need to only command soldiers (you can’t command the peasant), so you need to include a statement to the effect of, if friend.type == "soldier". I was able to get your code to work by just changing the last three lines of the commandSoldiers() function.

Thanks! It worked. (Now doing this to fit 20 characters)

can you help me, I tried doing as you said but the flower dosent grow to its full size

# 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 friend.type == "soldier":
                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()



Him Ive been trying to figure this out. can you help me

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 = hero.findNearestEnemy()
soldiers= hero.findByType(“soldier”)
for soldier in soldiers:
if friend.type == “soldier”:
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()
pickUpNearestCoin()
# commandSoldiers()
# pickUpNearestCoin()

Hi @hieveryone2, welcome to the CodeCombat Discourse! :tada:
Please could you format your code so I can see the indents:

Thanks
Danny

# 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 = hero.findNearestEnemy()
        soldiers= hero.findByType("soldier")
        for soldier in soldiers:
            if friend.type == "soldier":
                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()
    pickUpNearestCoin()
    # commandSoldiers()
    # pickUpNearestCoin()

Here is my code again. Thank you

Try a cut down version of

    def commandSoldiers():    
    # friends = hero.findFriends() # cut 
    # for friend in friends: # cut
        # enemy = hero.findNearestEnemy() # cut
        soldiers= hero.findByType("soldier")
        for soldier in soldiers:
            # if friend.type == "soldier": # cut
            enemy = soldier.findNearestEnemy() # insert
            if enemy: # insert
                hero.command(soldier, "attack",enemy)
1 Like

Thank you! But for some reason It still telling me that Is wrong. Is there anything else that you know is wrong with the rest of the code. That you would know

Thank you these has helped me so much. Thank you so so much.

:blush: