# Zoo Keeper Help!(Python)

**URL:** <https://discourse.codecombat.com/t/zoo-keeper-help-python/6480>\
**Category:** Uncategorized\
**Created:** [January 16, 2016, 7:12pm UTC](https://discourse.codecombat.com/t/zoo-keeper-help-python/6480 "2016-01-16T19:12:35Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![Thelightside](https://avatars.discourse-cdn.com/v4/letter/t/b782af/32.png) [@Thelightside](https://discourse.codecombat.com/u/Thelightside)\
**Post date:** [January 16, 2016, 7:12pm UTC](https://discourse.codecombat.com/t/zoo-keeper-help-python/6480/1 "2016-01-16T19:12:35Z")

</div>

I have been stuck on the level Zoo Keeper for a pretty long time now and any help would be greatly appreciated.  
My code:

```python
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

---

<div class="post-metadata">

**Author:** ![Luke10](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/luke10/32/19892_2.png) [@Luke10](https://discourse.codecombat.com/u/Luke10)\
**Post date:** [January 16, 2016, 10:11pm UTC](https://discourse.codecombat.com/t/zoo-keeper-help-python/6480/2 "2016-01-16T22:11:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Thelightside](https://avatars.discourse-cdn.com/v4/letter/t/b782af/32.png) [@Thelightside](https://discourse.codecombat.com/u/Thelightside)\
**Post date:** [January 16, 2016, 11:07pm UTC](https://discourse.codecombat.com/t/zoo-keeper-help-python/6480/3 "2016-01-16T23:07:17Z")

</div>

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.😔

---

<div class="post-metadata">

**Author:** ![AdrianCgw](https://avatars.discourse-cdn.com/v4/letter/a/ac8455/32.png) [@AdrianCgw](https://discourse.codecombat.com/u/AdrianCgw)\
**Post date:** [January 17, 2016, 12:55am UTC](https://discourse.codecombat.com/t/zoo-keeper-help-python/6480/4 "2016-01-17T00:55:14Z")

</div>

The last if

```python
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

---

<div class="post-metadata">

**Author:** ![Luke10](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/luke10/32/19892_2.png) [@Luke10](https://discourse.codecombat.com/u/Luke10)\
**Post date:** [January 17, 2016, 1:34pm UTC](https://discourse.codecombat.com/t/zoo-keeper-help-python/6480/5 "2016-01-17T13:34:38Z")

</div>

> [@Thelightside](#):
>
> 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.😔

No like does it say anything?

---

<div class="post-metadata">

**Author:** ![Thelightside](https://avatars.discourse-cdn.com/v4/letter/t/b782af/32.png) [@Thelightside](https://discourse.codecombat.com/u/Thelightside)\
**Post date:** [January 17, 2016, 7:16pm UTC](https://discourse.codecombat.com/t/zoo-keeper-help-python/6480/6 "2016-01-17T19:16:37Z")

</div>

No it does not show any errors.

---

<div class="post-metadata">

**Author:** ![Thelightside](https://avatars.discourse-cdn.com/v4/letter/t/b782af/32.png) [@Thelightside](https://discourse.codecombat.com/u/Thelightside)\
**Post date:** [January 17, 2016, 7:32pm UTC](https://discourse.codecombat.com/t/zoo-keeper-help-python/6480/7 "2016-01-17T19:32:48Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Luke10](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/luke10/32/19892_2.png) [@Luke10](https://discourse.codecombat.com/u/Luke10)\
**Post date:** [January 17, 2016, 10:16pm UTC](https://discourse.codecombat.com/t/zoo-keeper-help-python/6480/8 "2016-01-17T22:16:57Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Thelightside](https://avatars.discourse-cdn.com/v4/letter/t/b782af/32.png) [@Thelightside](https://discourse.codecombat.com/u/Thelightside)\
**Post date:** [January 17, 2016, 10:32pm UTC](https://discourse.codecombat.com/t/zoo-keeper-help-python/6480/9 "2016-01-17T22:32:00Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Luke10](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/luke10/32/19892_2.png) [@Luke10](https://discourse.codecombat.com/u/Luke10)\
**Post date:** [January 17, 2016, 11:08pm UTC](https://discourse.codecombat.com/t/zoo-keeper-help-python/6480/10 "2016-01-17T23:08:48Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Luke10](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/luke10/32/19892_2.png) [@Luke10](https://discourse.codecombat.com/u/Luke10)\
**Post date:** [January 17, 2016, 11:09pm UTC](https://discourse.codecombat.com/t/zoo-keeper-help-python/6480/11 "2016-01-17T23:09:41Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![AdrianCgw](https://avatars.discourse-cdn.com/v4/letter/a/ac8455/32.png) [@AdrianCgw](https://discourse.codecombat.com/u/AdrianCgw)\
**Post date:** [January 18, 2016, 1:16am UTC](https://discourse.codecombat.com/t/zoo-keeper-help-python/6480/12 "2016-01-18T01:16:49Z")

</div>

Try this:

```python
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:

```python
if something
     attack
if something_else
     move

```

This is good:

```python
if something 
    attack
elif something_else
    move

```

---

<div class="post-metadata">

**Author:** ![Thelightside](https://avatars.discourse-cdn.com/v4/letter/t/b782af/32.png) [@Thelightside](https://discourse.codecombat.com/u/Thelightside)\
**Post date:** [January 18, 2016, 2:27pm UTC](https://discourse.codecombat.com/t/zoo-keeper-help-python/6480/13 "2016-01-18T14:27:42Z")

</div>

Thanks, I finally beat the level.

---

<div class="post-metadata">

**Author:** ![Luke10](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/luke10/32/19892_2.png) [@Luke10](https://discourse.codecombat.com/u/Luke10)\
**Post date:** [January 18, 2016, 4:47pm UTC](https://discourse.codecombat.com/t/zoo-keeper-help-python/6480/14 "2016-01-18T16:47:26Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Thelightside](https://avatars.discourse-cdn.com/v4/letter/t/b782af/32.png) [@Thelightside](https://discourse.codecombat.com/u/Thelightside)\
**Post date:** [January 19, 2016, 8:56pm UTC](https://discourse.codecombat.com/t/zoo-keeper-help-python/6480/15 "2016-01-19T20:56:24Z")

</div>

It is okay.🙂

---

<div class="post-metadata">

**Author:** ![Ivaniuk\_Serhii](https://avatars.discourse-cdn.com/v4/letter/i/a9adbd/32.png) [@Ivaniuk\_Serhii](https://discourse.codecombat.com/u/Ivaniuk_Serhii)\
**Post date:** [May 22, 2016, 1:39pm UTC](https://discourse.codecombat.com/t/zoo-keeper-help-python/6480/16 "2016-05-22T13:39:20Z")

</div>

Hello)😃 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)

---

<div class="post-metadata">

**Author:** ![LAWYER](https://avatars.discourse-cdn.com/v4/letter/l/85e7bf/32.png) [@LAWYER](https://discourse.codecombat.com/u/LAWYER)\
**Post date:** [October 15, 2016, 5:56pm UTC](https://discourse.codecombat.com/t/zoo-keeper-help-python/6480/17 "2016-10-15T17:56:12Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Hellenar](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/hellenar/32/9100_2.png) [@Hellenar](https://discourse.codecombat.com/u/Hellenar)\
**Post date:** [April 16, 2017, 6:56pm UTC](https://discourse.codecombat.com/t/zoo-keeper-help-python/6480/19 "2017-04-16T18:56:30Z")

</div>

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