Play Home Contribute Github Join Us! Discourse Staff Members Github Contribution Guides Team

Zoo Keeper acting weird [Solved]

# Protect the cage.
# Put a soldier at each X.
points = []
points[0] = {"x": 33, "y": 42}
points[1] = {"x": 47, "y": 42}
points[2] = {"x": 33, "y": 26}
points[3] = {"x": 47, "y": 26}

while hero.gold <=80:
    coin = hero.findNearest(hero.findItems())
    if coin:
        hero.move(coin.pos)
for i in range(4):
    hero.summon("soldier")

while True:
    friends = hero.findFriends()
    for j in range(len(friends)):
        point = points[j]
        friend = friends[j]
        enemy =  friend.findNearestEnemy()
        if enemy and enemy.team == "ogres" and friend.distanceTo(enemy) < 5:
            hero.command(friend, "attack", enemy)
                
        if friend:
            hero.command(friend, "move", point)

I’m having some issues learning how to control troops. I’ve used “hero.command(friend, “move”, point)” to correctly move all of the troops, but like some other post I found, I was having the issue where my if loop never ends, thus my troops never being able to attack. using a break only moves one of my troops. If you could explain how to use break (or give me the level where it was explained) and a solution to that part of the code that would be great. Thanks!

(just a note im not asking for a solved code just what would end the if-loop after 4 runs through it)

This should be else:

1 Like

wait so just

        else:
            hero.command(friend, "move", point)

yup! (20 charactress)

1 Like

hmmm it continues to run

            hero.command(friend, "move", point)

YOu need a pass after here

You also need a pass after here

2 Likes

yep! thanks! completed it

3 Likes

No problem! (20chars)

1 Like

Click the solution box next to whichever post was most helpful. so eveyrone knows the most helpful comment

1 Like

And I already changed the title to [Solved].

2 Likes

ah ok, thanks 20 chars

1 Like

Still, it’s helpful right? the solution box to let others know the most helpful post?

2 Likes

Yes 100% @thebagel. (20)

2 Likes

@Xownfos So could you check the solution box under the most helpful post?

2 Likes

lmao sorry I wasn’t able to reply, I’m mostly doing codecombat during the week, and had other things to do during the weekend. Thanks for your help!

2 Likes

ok. no problem! (20 cahrs)

1 Like

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