Zoo Keeper help please

i have been stuck on zoo keeper for a little while now and this code dosent work

# 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 self.gold <=80:
    coin = self.findNearest(self.findItems())
    if coin:
        self.move(coin.pos)
for i in range(4):
    self.summon("soldier")

while True:
    friends = self.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:
            self.command(friend, "attack", enemy)
                
        if friend:
            self.command(friend, "move", point)

everything works well until the soldiers move to the X the dont attack at all

1 Like

Look at this section. When will that condition stop being true? In other words, when will the soldiers stop moving? Your friend will always be there (unless he dies).
If there are two if statements and they’re both true, the one which took precedence first will keep executing it’s action and won’t let anything else take place.
Correction (credit: xython):
“ If there are many if statements and they all are true, only the last will be executed ”
Danny

2 Likes

On another note, why are you using self? You used hero in all your other levels.
Perhaps you took it from somewhere else…Hmm.
If you don’t understand the concept then I’d prefer to explain it to you than for you to “cheat” your way through a vital level. (for loops are going to be coming up a lot further on in CoCo.
Danny

5 Likes

well all i had to do was add a break. that being said thanks Deadpool198!

3 Likes

I can’t get my soldiers to attack once they are at the points. here is my code:

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)
            

???(20 characters)