Zoo Keeper Help!(Python)

I have been stuck on the level Zoo Keeper for a pretty long time now and any help would be greatly appreciated.
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 self.gold <=80:
    coin = self.findNearest(self.findItems())
    if coin:
        self.move(coin.pos)
for i in range(4):
    self.summon("soldier")
loop:
    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)

My friends do not attack
Thanks

Does it say anything? Like “fix your code” or “error on line 2” or something?

There are no errors. My Friends just go to the points and don’t attack the ogres. It just tries to keep on going forward but it gets stuck.:pensive:

The last if

if friend:
            self.command(friend, "move", point)

is always true so every attack command will be followed by a “move” command which overwrites the attack command.
attack + move = move only

No like does it say anything?

No it does not show any errors.

@AdrianCgw Where in my code does it make my if-statement true?

I’m not sure what’s wrong other then what @AdrianCgw mentioned. Is there any more information you can give us?

What other info. can I give @Luke10?Also can you please tell me how to do what @AdrianCgw said.

I not really sure how do do what @AdrianCgw said. I’ll invite @nick

He probably can help. I’m sorry I can’t assist you. :cry:

Try this:

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

Shortly, this way of codding is bad:

if something
     attack
if something_else
     move

This is good:

if something 
    attack
elif something_else
    move
5 Likes

Thanks, I finally beat the level.

1 Like

Glad to hear it. Sorry I wasn’t much help.

1 Like

It is okay.:slightly_smiling:

1 Like

Hello):smiley: i have some question on this level’s code… i passed it, but still have one interest)
So, can i, when my 4 soldiers took already their positions and started attack enemies, command them to take position of another defined soldier (while primary code executing)? Other words, can i direct defined soldier from his own position to other defined position of other soldier? Is it real to modernise this level’s code this way?
It is will be useful when orcs attack from only one side)

In your original code change the if friend part to else, because it just makes them go to the spots instead of attacking

1 Like

Please do not revive dead topics. This one has been left alone for 6 months. :slight_smile:

2 Likes