[SOLVED] Two Flowers Python


#1

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


#2

Help? Someone? Please?


#3

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.


#4

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