[SOLVED]Two Flowers codecombat help

Code:

# If the peasant is damaged, the flowers will shrink!
while True:
    enemy = hero.findNearestEnemy()

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:
    enemy = hero.findNearestEnemy()
    summonSoldiers()
    commandSoldiers()
    pickUpNearestCoin()
    # commandSoldiers()
    # pickUpNearestCoin()
    if (enemy and hero.distanceTo(enemy)<=5):
        if hero.isReady("cleave"):
            hero.cleave(enemy)
        elif (hero.isReady("bash")):
            hero.bash(enemy)
        else:
            hero.shield()
            hero.attack(enemy)

Yeet, I think the problem with your code is this:

I don’t think that you actually need this, it just complicates things. Try using the while loop just to summonSoldiers(), commandSoldiers(), and pickUpNearestCoin(). Hope this helps!

1 Like

So like that, wait it still didnt work but is there a way to solve it with this gear:  Runesword Deflector boots of jumping boss star 1 promatticon 4 tarnished bronze chestplate and all the other stuff the game makes u buy?
My character gets 1139 hp from the gear, is there a way to work it out with this kind of equipment?

The problem is this. While True loops never end and cannot run concurrently (in this level). At the moment you’re getting stuck on that while True loop and nothing else is running because you haven’t told it to stop.
Danny

how do i make it stop and when do I? Sorry I’m just confused

You could make it stop with break:
e.g.

if hero.time >= 5:
    break

Or you could just not use it. I don’t see why you would actuallly.

I got rid of it but I still failed

This function is a bit confused at the moment. You’re using two for loops (nooo don’t do it!!). Except for looping through two/three+ dimensional arrays, you really don’t need more than one (in CoCo at least).
You’ve checked whether the friend is a “soldier”, so why loop through the soldiers as well.
You can get rid of two lines and that function will work.
Danny

So how would it look?

If I show you you won’t learn anything.
You need one for loop.
You need it to command soldiers.
You have a soldiers array and a for loop which goes through it.
Why do you have a friend for loop and the line which checks if the friend is a soldier? The soldier for loop does that for you.
Danny

I still don’t get it

This is a for loop:

array = ["apple", "banana", "pear", "orange"]
for item in array:
    hero.say(item)

Output:

#-- "apple"
#-- "banana"
#-- "pear"
#-- "orange"

It loops through the array one by one and uses the item (in this case it prints a string).
Soldiers is an array.
So you loop through it:

for soldier in soldiers:

Then you use the variable you’ve made (soldier). In this case you command it to attack it’s nearest enemy. Which you’ll need to define inside the for loop.
So what are these lines?

You’re getting confused inbetween two possible (and both perfectly valid) ways of doing this. You’ll have to chose one.
If you need more help review earlier commanding levels.
Danny

I got rid of friend type; yet I still fail

Did you also get rid of the friends for loop?
Please could you post your new code.
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():
    friend = hero.findFriends()
    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:
    enemy = hero.findNearestEnemy()
    summonSoldiers()
    commandSoldiers()
    pickUpNearestCoin()
    # commandSoldiers()
    # pickUpNearestCoin()


And yet:

You don’t need that line.

# 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)
    
peasant = hero.findByType("peasant")[0]

while True:
    enemy = hero.findNearestEnemy()
    summonSoldiers()
    commandSoldiers()
    pickUpNearestCoin()
    # commandSoldiers()
    # pickUpNearestCoin()

still  doesnt work

nvm I redid the entire level with a slightly different code and it worked

1 Like

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.