Need help with: Forest, Backwoods Brawl4

I made it to round 4, but now am stuck. I’ve tried to resolve this several ways, but no luck:

The code never progresses past the friends = line, so no idea if one of my attempts (shown) even helps. Code:

def commandTroops():
    friends = hero.findFriends()
    if friends == None:
        pass
    else:
        for x in len(friends):
            friend = friends[x]
            if friend.type == "paladin":
                CommandPaladin(friend)
            elif friend.type == "soldier" or friend.type == "archer" or friend.type == "griffin-rider":
                CommandSoldier(friend)
            elif friend.type == "peasant":
                CommandPeasant(friend)

I’m betting it’s something simple, but darned if I see it. Oh…at this point, I no longer have any friends left alive and not yet enough gold to summon more.

What will be the value of friends if there are no friends?

friends = hero.findFriends()

see hero.findEnemies() Quiz

Heh, that’s my problem…no friends :wink:

I tried the link you provided and replaced with the specified code, but it returns ‘so many enemies’…It’s seeing the wiz as an enemy?

I did a bit of testing and the only thing I could do to stop the error message: “TypeError: Need an object” was to change this line:

for x in len(friends):

This is incorrect (I’m pretty sure).
You can do:

for unit in units:
    #code

or (I think more commonly used in python):

for i in range(0, len(units)):
    unit = units[i]
    #code

This may have been what @Xython was saying but I’m not sure.
I hope this helps anyway.

1 Like

Deadpool is right about this error. But I think your main error is elsewhere:
Type this code in completely void level (https://codecombat.com/play/level/forest-flower-grove?)
( I suppose you have the most advanced glaces)

items = hero.findItems();
friends = hero.findFriends()
enemies = hero.findEnemies()
ogres = hero.findByType("ogre")
friendlyMissiles = hero.findFriendlyMissiles()
enemyMissiles = hero.findEnemyMissiles()
hazards = hero.findHazards()
if items and friends and enemies and ogres and friendlyMissiles and enemyMissiles and hazards:
    hero.say("Maybe they are all of Dark Material")
else:
    hero.say("Nothing here")

If the result


is unexpected for you read carefully the whole hero-findenemies-quiz topic

Ok, thanks @Deadpool198 and @xython! Yes, I have the best glasses…I will try these suggestions out and see where it leads me. :slight_smile:

Ok…got past that error!

@Deadpool198 thanks…changing to the common python method was the trick.

@xython Thanks again…after trying it with both the enchanted lenses and twilight glasses, I saw dark matter both times, I think I realized your point. I modified the code to utilize the ‘for i in range’ method, for each unit type and I never saw a thing. Lesson well learned :smiley:

1 Like