Pender's Trial Python Help!

My code works perfectly with no bugs, but my peasant seems to be limited to some distance near my hero, thus limiting the amount of gold I can collect. Is there a way to fix this? My hero doesn’t get enough gold to hire soldiers!
Here is my code:

# Pender wants to test you on a series of trials. Use your boss star to clear off the ogres! Remember, you cannot move or attack in this level.
def summonTroops():
    # These are just an example. Feel free to use griffin riders and/or other units!
    if hero.gold >= 100:
        hero.summon("soldier")
        hero.summon("archer")
while True:
    summonTroops()
    friends = hero.findFriends()
    # Iterate over all troops using a for loop. Make peasants collect coins. Combat troops fight. 
    item = hero.findNearestItem()
    enemy = hero.findNearestEnemy()
    flag = hero.findFlag("green")
    
    for friend in friends:
        #if friend.type == "peasant" and item:
        #    hero.command(friend, "move", item.pos)
        if friend.type == "peasant":
            hero.command(friend, "move", item.pos)
        elif friend.type != "peasant":
            if enemy:
                hero.command(friend, "attack", enemy)
            else:
                hero.command(friend, "move", Vector(30, 36))
        elif enemy and enemy.type == "shaman":
            hero.command(friend, "attack", enemy)

I also tried it if friend.type == "peasant" and item: and it had the same outcome as if friend.type == "peasant":

You want to first find a peasant, then change item = hero.findNearestItem() to item = peasant.findNearestItem(), so that will make the peasant find the nearest item to the peasant

peasant = hero.findByType("peasant")
item = peasant.findNearestItem()

peasant has no method findNearestItem()

This will create a list of ALL the peasants. You should put peasant.findNearestItem() in the for friend in friends loop before the command because it’s a specific peasant instead of all of them

    for friend in friends:
        peasant = hero.findByType("peasant")
        item = peasant.findNearestItem()
        if friend.type == "peasant":
            if item:
                hero.command(friend, "move", item.pos)

like this?

Oh, wait, I needed to do it like this…

for friend in friends:
        peasants = hero.findByType("peasant")
        for peasant in peasants:
            item = peasant.findNearestItem()
            if friend.type == "peasant":
                if item:
                    hero.command(friend, "move", item.pos)

it worked! But now it’s giving me another error in the bottom of my code:

if friend.type != "peasant":
            if enemy:
                hero.command(friend, "attack", enemy)
            else:
                hero.command(friend, "move", Vector(30, 36))
        elif enemy and enemy.type == "shaman":
            hero.command(friend, "attack", enemy)

Victor (the peasant) has no “attack” command

yep thats accurate. Peasants cannot attack

Oh wait, I got rid of the bottom two lines and now everything works like it’s supposed to, yes! But the shamans keep killing me… I need to target them first

here, you can just write else because they either are a peasant, or aren’t one

Oh, good idea! (20 chars)

Wait, that didn’t work because it gave me this error: “else: for-else stated unsupported”

you can write something like if enemy.type == shaman:

you prob indented incorrectly

Oh, wait, I didn’t indent incorrectly…
Here, this is my entire code now:

def summonTroops():
    # These are just an example. Feel free to use griffin riders and/or other units!
    if hero.gold >= 40:
        hero.summon("soldier")
        hero.summon("archer")
while True:
    summonTroops()
    friends = hero.findFriends()
    # Iterate over all troops using a for loop. Make peasants collect coins. Combat troops fight. 
    #item = hero.findNearestItem()
    enemy = hero.findNearestEnemy()
    flag = hero.findFlag("green")
    
    for friend in friends:
        peasants = hero.findByType("peasant")
        for peasant in peasants:
            item = peasant.findNearestItem()
            if friend.type == "peasant":
                if item:
                    hero.command(friend, "move", item.pos)
        if friend.type != "peasant":
            if enemy:
                if enemy.type == "shaman":
                    hero.command(friend, "attack", enemy)
            
            else:
                hero.command(friend, "move", Vector(30, 36))
#        elif enemy and enemy.type == "shaman":
#            hero.command(friend, "attack", enemy)

you don’t need this because you’re using friend as your variable and not peasant. in
item = peasant.findNearestItem() you can just use friend instead of peasant

instead of this, make an array shamans that says shamans = hero.findByType("shaman") and then use a for loop in conjuction with an if shaman: statement
like

shamans = hero.findByType("shaman")
for shaman in shamans:
    if shaman:
        hero.command(friend, "attack", shaman)
    else:
        hero.command(friend, "attack", enemy)
for friend in friends:
        item = friend.findNearestItem()

in the item = friend.findNearestItem() it’s giving me an error “did you mean friend.findNearestEnemy?”

but if i go back to for peasant in peasants it doesn’t give me the error

update: the for shaman in shamans works but my friends target only the shamans, so if there isn’t a shaman they stand around doing nothing