# Help with Sarven Shepherd (python) Solved

**URL:** <https://discourse.codecombat.com/t/help-with-sarven-shepherd-python-solved/13314>\
**Category:** Level Help\
**Created:** [December 19, 2017, 7:00pm UTC](https://discourse.codecombat.com/t/help-with-sarven-shepherd-python-solved/13314 "2017-12-19T19:00:56Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Atticus\_Black](https://avatars.discourse-cdn.com/v4/letter/a/f1d935/32.png) [@Atticus\_Black](https://discourse.codecombat.com/u/Atticus_Black)\
**Post date:** [December 19, 2017, 7:00pm UTC](https://discourse.codecombat.com/t/help-with-sarven-shepherd-python-solved/13314/1 "2017-12-19T19:00:56Z")

</div>

I can not figure out this infinite loop problem please help  
while True:  
enemies = hero.findEnemies()  
enemyIndex = 1  
while enemyIndex \< len(enemies):  
enemy = enemies[enemyIndex]  
if enemy.type != “sand-yak”:  
hero.attack(enemy)  
if enemy.health \> 0:  
hero.attack(enemy)  
else:  
enemyIndex += 1  
pass  
hero.moveXY(40, 32)

---

<div class="post-metadata">

**Author:** ![MunkeyShynes](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/munkeyshynes/32/9277_2.png) [@MunkeyShynes](https://discourse.codecombat.com/u/MunkeyShynes)\
**Post date:** [December 19, 2017, 7:52pm UTC](https://discourse.codecombat.com/t/help-with-sarven-shepherd-python-solved/13314/2 "2017-12-19T19:52:03Z")

</div>

I can’t see the format of your code because you didn’t post it properly but there are some discernible errors.

First is the assignment of enemyIndex = 1. enemyIndex should start at 0 and then increment by 1.  
Second, the first hero.attack should be after checking enemy.health and the second hero.attack isn’t necessary.

You will have to post it correctly in order for anyone to see the structure. Please post correctly from now on. It’s really easy and helps us help you. Thanks.

![Button01](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/2X/2/23571e359fbb18b07ae75a4c1de2b2981f730d2f.JPG)

---

<div class="post-metadata">

**Author:** ![KyloRen9700](https://avatars.discourse-cdn.com/v4/letter/k/3da27b/32.png) [@KyloRen9700](https://discourse.codecombat.com/u/KyloRen9700)\
**Post date:** [December 19, 2017, 8:53pm UTC](https://discourse.codecombat.com/t/help-with-sarven-shepherd-python-solved/13314/3 "2017-12-19T20:53:09Z")

</div>

Maybe this helps out:

```python
while True:
enemies = hero.findEnemies()
enemyIndex = 1
while enemyIndex < len(enemies):
enemy = enemies[enemyIndex]
if enemy.type != “sand-yak”:
hero.attack(enemy)
if enemy.health > 0:
hero.attack(enemy)
else:
enemyIndex += 1
pass
hero.moveXY(40, 32)

```

---

<div class="post-metadata">

**Author:** ![KyloRen9700](https://avatars.discourse-cdn.com/v4/letter/k/3da27b/32.png) [@KyloRen9700](https://discourse.codecombat.com/u/KyloRen9700)\
**Post date:** [December 19, 2017, 8:53pm UTC](https://discourse.codecombat.com/t/help-with-sarven-shepherd-python-solved/13314/4 "2017-12-19T20:53:43Z")

</div>

That’s better. Now we can see your code and help out

---

<div class="post-metadata">

**Author:** ![MunkeyShynes](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/munkeyshynes/32/9277_2.png) [@MunkeyShynes](https://discourse.codecombat.com/u/MunkeyShynes)\
**Post date:** [December 19, 2017, 9:12pm UTC](https://discourse.codecombat.com/t/help-with-sarven-shepherd-python-solved/13314/5 "2017-12-19T21:12:03Z")

</div>

Well, you know how to do it now. But you copied it from here, not the game. It has to be copied from the game in order to have the formatting. When copying it from this board it has no formatting and there’s no indentations. You’re getting closer.

Also, in the previous post I discussed starting your enemyIndex at 0, not 1. You are starting your index at the second object in the array. Python starts counting at 0, not 1.

Three objects in an array. They are numbered, 0, 1, 2.

The first hero.attack should be after checking enemy.health and the second hero.attack isn’t necessary.

The else statement should be deleted.

---

<div class="post-metadata">

**Author:** ![Atticus\_Black](https://avatars.discourse-cdn.com/v4/letter/a/f1d935/32.png) [@Atticus\_Black](https://discourse.codecombat.com/u/Atticus_Black)\
**Post date:** [December 19, 2017, 10:24pm UTC](https://discourse.codecombat.com/t/help-with-sarven-shepherd-python-solved/13314/6 "2017-12-19T22:24:57Z")

</div>

when I put it at zero it will not attack anything what should I do

---

<div class="post-metadata">

**Author:** ![MunkeyShynes](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/munkeyshynes/32/9277_2.png) [@MunkeyShynes](https://discourse.codecombat.com/u/MunkeyShynes)\
**Post date:** [December 19, 2017, 11:03pm UTC](https://discourse.codecombat.com/t/help-with-sarven-shepherd-python-solved/13314/7 "2017-12-19T23:03:56Z")

</div>

You have to post your code - copied from the game to here - in order for us to see your structure. If you’ve made the changes detailed above and the structure is correct, it will work. When you click on the button indicated above, it will tell you where to paste your code. It’s really easy.

---

<div class="post-metadata">

**Author:** ![Atticus\_Black](https://avatars.discourse-cdn.com/v4/letter/a/f1d935/32.png) [@Atticus\_Black](https://discourse.codecombat.com/u/Atticus_Black)\
**Post date:** [December 20, 2017, 3:25am UTC](https://discourse.codecombat.com/t/help-with-sarven-shepherd-python-solved/13314/8 "2017-12-20T03:25:39Z")

</div>

```python
while True:
    enemies = hero.findEnemies()
    enemyIndex = 1
    while enemyIndex < len(enemies):
        enemy = enemies[enemyIndex]
        if enemy.type != "sand-yak":
            hero.attack(enemy)
            if enemy.health > 0:
                hero.attack(enemy)
            else:
                enemyIndex += 1
        pass
    hero.moveXY

```

---

<div class="post-metadata">

**Author:** ![MunkeyShynes](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/munkeyshynes/32/9277_2.png) [@MunkeyShynes](https://discourse.codecombat.com/u/MunkeyShynes)\
**Post date:** [December 20, 2017, 12:03pm UTC](https://discourse.codecombat.com/t/help-with-sarven-shepherd-python-solved/13314/9 "2017-12-20T12:03:00Z")

</div>

Thank you for posting your code correctly. However, you didn’t make any of the three changes that I told you need to be fixed. 🤔

---

<div class="post-metadata">

**Author:** ![Atticus\_Black](https://avatars.discourse-cdn.com/v4/letter/a/f1d935/32.png) [@Atticus\_Black](https://discourse.codecombat.com/u/Atticus_Black)\
**Post date:** [December 20, 2017, 1:10pm UTC](https://discourse.codecombat.com/t/help-with-sarven-shepherd-python-solved/13314/10 "2017-12-20T13:10:39Z")

</div>

there is still an infinite loop

```python
while True:
    enemies = hero.findEnemies()
    enemyIndex = 0
    while enemyIndex < len(enemies):
        enemy = enemies[enemyIndex]
        if enemy.type != "sand-yak":
            if enemy.health > 0:
                hero.attack(enemy)
            enemyIndex += 1
        pass
    hero.moveXY(40, 32)

```

---

<div class="post-metadata">

**Author:** ![MunkeyShynes](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/munkeyshynes/32/9277_2.png) [@MunkeyShynes](https://discourse.codecombat.com/u/MunkeyShynes)\
**Post date:** [December 20, 2017, 1:55pm UTC](https://discourse.codecombat.com/t/help-with-sarven-shepherd-python-solved/13314/11 "2017-12-20T13:55:20Z")

</div>

The line to increment enemyIndex += 1 is tabbed over too far. It should line up under the if enemy.type conditional statement.

---

<div class="post-metadata">

**Author:** ![Atticus\_Black](https://avatars.discourse-cdn.com/v4/letter/a/f1d935/32.png) [@Atticus\_Black](https://discourse.codecombat.com/u/Atticus_Black)\
**Post date:** [December 20, 2017, 2:47pm UTC](https://discourse.codecombat.com/t/help-with-sarven-shepherd-python-solved/13314/12 "2017-12-20T14:47:12Z")

</div>

thank you for helping me with the infinite loop problem

---

<div class="post-metadata">

**Author:** ![Sharknado](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/sharknado/32/9124_2.png) [@Sharknado](https://discourse.codecombat.com/u/Sharknado)\
**Post date:** [September 19, 2018, 1:27pm UTC](https://discourse.codecombat.com/t/help-with-sarven-shepherd-python-solved/13314/13 "2018-09-19T13:27:25Z")

</div>

Can someone help me

```python
while True:
    enemies = hero.findEnemies()
    enemyIndex = 0
    enemies = hero.findNearestEnemy()

     # Wrap this logic in a while loop to attack all enemies.
    # Find the array's length with: len(enemies)
while True:
    while enemyIndex < len(enemies):
        enemy = enemies[enemyIndex]
        # "!=" means "not equal to."
        if enemy.type != "sand-yak":
            # While the enemy's health is greater than 0, attack it!
            while enemy.health > 0:
                hero.attack(enemy)
        enemyIndex += 1
        pass
                
# Between waves, move back to the center.
hero.moveXY(40, 33)

```

hero won’t move

---

<div class="post-metadata">

**Author:** ![Sharknado](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/sharknado/32/9124_2.png) [@Sharknado](https://discourse.codecombat.com/u/Sharknado)\
**Post date:** [September 19, 2018, 1:36pm UTC](https://discourse.codecombat.com/t/help-with-sarven-shepherd-python-solved/13314/14 "2018-09-19T13:36:23Z")

</div>

never mind figured it out

---

<div class="post-metadata">

**Author:** ![Michael\_Osborne](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/michael_osborne/32/10772_2.png) [@Michael\_Osborne](https://discourse.codecombat.com/u/Michael_Osborne)\
**Post date:** [October 4, 2019, 5:42pm UTC](https://discourse.codecombat.com/t/help-with-sarven-shepherd-python-solved/13314/16 "2019-10-04T17:42:09Z")

</div>

```python
# Use while loops to pick out the ogre

while True:
    enemies = hero.findEnemies()
    enemyIndex = 0
    
    # Wrap this logic in a while loop to attack all enemies.
    # Find the array's length with: len(enemies)
    while enemy == len(enemies):
        enemy = enemies[enemyIndex]
    # "!=" means "not equal to."
    if enemy.type != "sand-yak":
        # While the enemy's health is greater than 0, attack it!
        while enemy.health > 0:
            hero.attack(enemy)
            pass
        
        # Between waves, move back to the center.
    else:
        hero.moveXY(40, 32)
    

```

My hero has a error it said property of ‘type’ is not defined

---

<div class="post-metadata">

**Author:** ![Deadpool198](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/deadpool198/32/16753_2.png) [@Deadpool198](https://discourse.codecombat.com/u/Deadpool198)\
**Post date:** [October 5, 2019, 6:10pm UTC](https://discourse.codecombat.com/t/help-with-sarven-shepherd-python-solved/13314/17 "2019-10-05T18:10:59Z")

</div>

You need to check _whether_ there **is** an enemy before you check it’s type.  
-Danny

---

<div class="post-metadata">

**Author:** ![Kip\_Coleman](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/kip_coleman/32/10854_2.png) [@Kip\_Coleman](https://discourse.codecombat.com/u/Kip_Coleman)\
**Post date:** [October 13, 2019, 3:02am UTC](https://discourse.codecombat.com/t/help-with-sarven-shepherd-python-solved/13314/18 "2019-10-13T03:02:37Z")

</div>

I’m having a problem with this level as well. My character tries to fight off the enemies, but just dies. Fails to “humans must survive.”

```python
# Use while loops to pick out the ogre

while True:
    enemies = hero.findEnemies()
    enemyIndex = 0

    # Wrap this logic in a while loop to attack all enemies.
    # Find the array's length with: len(enemies)
    while enemyIndex < len(enemies):
        enemy = enemies[enemyIndex]
        # "!=" means "not equal to."
        if enemy.type != "sand-yak":
            while enemy.health > 0:
                hero.attack(enemy)
        enemyIndex += 1
    # Between waves, move back to the center.
    hero.moveXY(40, 32)

```

Any assitance you can provide would be appreciated! Thanks.

---

<div class="post-metadata">

**Author:** ![Deadpool198](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/deadpool198/32/16753_2.png) [@Deadpool198](https://discourse.codecombat.com/u/Deadpool198)\
**Post date:** [October 13, 2019, 10:07am UTC](https://discourse.codecombat.com/t/help-with-sarven-shepherd-python-solved/13314/19 "2019-10-13T10:07:51Z")

</div>

Hi, welcome to the CodeCombat discourse.  
Your code works fine for me, so it must be an equipment problem. If possible try and find the best armour/weapons for this level or play other levels to get gems to get better equipment.  
If neither the first not the second option works, could you post a screenshot of your best equipment (don’t forget about rings, special abilities etc.) and another of the level screen.  
Thanks,  
-Danny

---

<div class="post-metadata">

**Author:** ![TheCoder](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/thecoder/32/11141_2.png) [@TheCoder](https://discourse.codecombat.com/u/TheCoder)\
**Post date:** [December 13, 2019, 2:27am UTC](https://discourse.codecombat.com/t/help-with-sarven-shepherd-python-solved/13314/20 "2019-12-13T02:27:27Z")

</div>

You should maybe get better armor and better sword.

---

<div class="post-metadata">

**Author:** ![strongster](https://avatars.discourse-cdn.com/v4/letter/s/9e8a1a/32.png) [@strongster](https://discourse.codecombat.com/u/strongster)\
**Post date:** [March 18, 2020, 10:01pm UTC](https://discourse.codecombat.com/t/help-with-sarven-shepherd-python-solved/13314/21 "2020-03-18T22:01:53Z")

</div>

Hi there, my code isn’t working. I came up with this. Here’s my code. It keeps on getting stuck in an infinite loop.

# Use while loops to pick out the ogre

while True:  
enemies = hero.findEnemies()  
enemyIndex = 0

```
# Wrap this logic in a while loop to attack all enemies.
# Find the array's length with: len(enemies)
while enemyIndex < len(enemies):
    enemy = enemies[enemyIndex]
    # "!=" means "not equal to."
    if enemy.type != "sand-yak":
        # While the enemy's health is greater than 0, attack it!
        while enemy.health > 0:
            hero.attack(enemy)
    pass

# Between waves, move back to the center.
hero.moveXY(40, 32)
```

[Next page](https://discourse.codecombat.com/t/help-with-sarven-shepherd-python-solved/13314.md?page=2)
