# \[SOLVED\] Can't solve Sarven Shepherd

**URL:** <https://discourse.codecombat.com/t/solved-cant-solve-sarven-shepherd/14966>\
**Category:** Solved\
**Created:** [June 14, 2018, 1:41am UTC](https://discourse.codecombat.com/t/solved-cant-solve-sarven-shepherd/14966 "2018-06-14T01:41:15Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tyreak\_Chapman](https://avatars.discourse-cdn.com/v4/letter/t/eb8c5e/32.png) [@Tyreak\_Chapman](https://discourse.codecombat.com/u/Tyreak_Chapman)\
**Post date:** [June 14, 2018, 1:41am UTC](https://discourse.codecombat.com/t/solved-cant-solve-sarven-shepherd/14966/1 "2018-06-14T01:41:16Z")

</div>

I’ve been trying to solve this for weeks with no luck, and I’ve almost every single thing possible but it yielded the same results. If there’s something I’m doing wrong, please tell me.

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

```

---

<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:** [June 14, 2018, 2:20pm UTC](https://discourse.codecombat.com/t/solved-cant-solve-sarven-shepherd/14966/2 "2018-06-14T14:20:35Z")

</div>

The code will only iterate through a single time because you aren’t incrementing. Also, there is a structural problem. The line, `while enemyIndex < len(enemies):` is not inside the `while True:` loop (it should be).

---

<div class="post-metadata">

**Author:** ![Tyreak\_Chapman](https://avatars.discourse-cdn.com/v4/letter/t/eb8c5e/32.png) [@Tyreak\_Chapman](https://discourse.codecombat.com/u/Tyreak_Chapman)\
**Post date:** [June 14, 2018, 3:05pm UTC](https://discourse.codecombat.com/t/solved-cant-solve-sarven-shepherd/14966/3 "2018-06-14T15:05:46Z")

</div>

Thank you! I was able to solve the level.

---

<div class="post-metadata">

**Author:** ![Angad\_Tendulkar](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/angad_tendulkar/32/9995_2.png) [@Angad\_Tendulkar](https://discourse.codecombat.com/u/Angad_Tendulkar)\
**Post date:** [May 29, 2019, 6:45pm UTC](https://discourse.codecombat.com/t/solved-cant-solve-sarven-shepherd/14966/4 "2019-05-29T18:45:15Z")

</div>

This is too slow

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

```

---

<div class="post-metadata">

**Author:** ![brooksy125](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/brooksy125/32/9966_2.png) [@brooksy125](https://discourse.codecombat.com/u/brooksy125)\
**Post date:** [May 29, 2019, 8:06pm UTC](https://discourse.codecombat.com/t/solved-cant-solve-sarven-shepherd/14966/5 "2019-05-29T20:06:41Z")

</div>

That is because you currently have an infinite loop. Remember that your second while loop is looking for a condition. In this instance it is whether or not:

```python
enemyIndex < len(enemies) # if number in enemyIndex is less than the count of enemies

```

Your enemyIndex starts at Zero which is less than the length of enemies (or count of enemies in a general sense), at what point in your code do you increase your enemyIndex to eventually go through all of the enemies? Your enemyIndex helps you walk through all the enemies in the array (enemies) to compare each one at a time. If you need more help, below is another post that covers the topic a little more.

> [@Sarven Sheperd help (Python)](https://discourse.codecombat.com/t/sarven-sheperd-help-python/18494/2):
>
> Ok, right now your code has only identified one enemy enemy = enemies[enemyIndex] which is the correct approach. You are just missing a few other pieces That enemy is likely a “sand-yak”, so it won’t attack and does nothing. I’ll try to explain the concept with more direction, but won’t actually show you code yet. To find other enemies, you need to increase your enemyIndex to step through the enemies array. You do this in a while loop with a condition. In this case, the condition should…

---

<div class="post-metadata">

**Author:** ![Angad\_Tendulkar](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/angad_tendulkar/32/9995_2.png) [@Angad\_Tendulkar](https://discourse.codecombat.com/u/Angad_Tendulkar)\
**Post date:** [June 18, 2019, 1:52pm UTC](https://discourse.codecombat.com/t/solved-cant-solve-sarven-shepherd/14966/6 "2019-06-18T13:52:54Z")

</div>

I just removed this line of code and it worked `:D`. Plus I can never understand why if it is not a sand yak it doesn’t move on to the next closest enemy!

---

<div class="post-metadata">

**Author:** ![Angad\_Tendulkar](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/angad_tendulkar/32/9995_2.png) [@Angad\_Tendulkar](https://discourse.codecombat.com/u/Angad_Tendulkar)\
**Post date:** [June 18, 2019, 1:59pm UTC](https://discourse.codecombat.com/t/solved-cant-solve-sarven-shepherd/14966/7 "2019-06-18T13:59:04Z")

</div>

DYK that the code

```python
enemyIndex = 0
for enemy in enemies:
    hero.attack(enemy)

```

would do this entire loop through the array index for me!!!  
so much easier!

---

<div class="post-metadata">

**Author:** ![brooksy125](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/brooksy125/32/9966_2.png) [@brooksy125](https://discourse.codecombat.com/u/brooksy125)\
**Post date:** [June 18, 2019, 3:07pm UTC](https://discourse.codecombat.com/t/solved-cant-solve-sarven-shepherd/14966/8 "2019-06-18T15:07:35Z")

</div>

You are correct. A for loop will get the same results with this problem and I prefer to use that option if I can because it is quite simple.

Although, you didn’t learn the structure of the while loop with the condition which will be used in a few levels coming up. The only detail you were missing in your first code is to increment (increase) the index by 1 after every loop.

```python
enemyIndex +=1

```

There are certain situations where a for loop won’t work well and you will need to use the while loop with a condition. Below is the basic structure for a while condition loop using the index method.

```python
index = 0
while index < len(arrayVariable): # later you will need the range(len(arrayVariable)) for certain levels. Will learn range() later.
    object = arrayVariable[index] # the index gives the position number for the array
    # add the rest of your code to loop through
    index += 1 # at the end to change to the next position in the array
```

---

<div class="post-metadata">

**Author:** ![quackpear534](https://avatars.discourse-cdn.com/v4/letter/q/ecd19e/32.png) [@quackpear534](https://discourse.codecombat.com/u/quackpear534)\
**Post date:** [July 18, 2019, 6:35pm UTC](https://discourse.codecombat.com/t/solved-cant-solve-sarven-shepherd/14966/9 "2019-07-18T18:35:09Z")

</div>

My enemy.type isn’t working. do you have to have a certain item on?

---

<div class="post-metadata">

**Author:** ![brooksy125](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/brooksy125/32/9966_2.png) [@brooksy125](https://discourse.codecombat.com/u/brooksy125)\
**Post date:** [July 18, 2019, 8:20pm UTC](https://discourse.codecombat.com/t/solved-cant-solve-sarven-shepherd/14966/10 "2019-07-18T20:20:08Z")

</div>

No specific item is needed for enemy.type that I know of. Are you working on this same level?

There are two parts to the enemy.type.

1. You have to create a variable name, in this case enemy. (The variable name can be anything)

```python
enemy = hero.findNearestEnemy()
#or
for enemy in enemies

```

1. You need to know the different types of enemy to compare and make sure they are in quotations

```python
"sand-yak", "munchkin", "shaman"

```

When you click on the enemy, it will show you their type next to their name.

![type](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/2X/d/df74de32ee47340608ce00ba38db71b22ae91c7a.jpeg)

---

<div class="post-metadata">

**Author:** ![albert](https://avatars.discourse-cdn.com/v4/letter/a/48db29/32.png) [@albert](https://discourse.codecombat.com/u/albert)\
**Post date:** [January 22, 2020, 11:18am UTC](https://discourse.codecombat.com/t/solved-cant-solve-sarven-shepherd/14966/11 "2020-01-22T11:18:16Z")

</div>

Hi I don’t find where is the problem… thanks for your time

```python
return #Commented out to stop 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.
    
    while enemyIndex < len(enemies):
        # Find the array's length with: 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, 30)
    

```

---

<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:** [January 22, 2020, 3:13pm UTC](https://discourse.codecombat.com/t/solved-cant-solve-sarven-shepherd/14966/12 "2020-01-22T15:13:03Z")

</div>

Hi @albert, I can see two problems here:

1. is here:

```python
# While the enemy's health is greater than 0, attack it!
while enemy.health < 0:

```

It’s just a simple typo, you’ll probably be able to see it now. (p.s. it involves what the comment says. And if you think about it your code doesn’t make sense, why attack a dead enemy?)  
2. the placement of this line:

> [@albert](#):
>
> enemyIndex += 1

if you put it there what happens if the enemy is a sand yak? That’s why you’ve got an infinite loop, you need to put it somewhere else (same line but different level of indentation).  
I hope this helps,  
Danny

---

<div class="post-metadata">

**Author:** ![albert](https://avatars.discourse-cdn.com/v4/letter/a/48db29/32.png) [@albert](https://discourse.codecombat.com/u/albert)\
**Post date:** [January 26, 2020, 10:13am UTC](https://discourse.codecombat.com/t/solved-cant-solve-sarven-shepherd/14966/13 "2020-01-26T10:13:52Z")

</div>

hi deadpool198 thanks for your answer

I changed the \> symbol but I didn’t understand where I should put enemyIndex… I tried on a lot of places but … nothing, The point is that I didn’t really understand how it works…

---

<div class="post-metadata">

**Author:** ![dedreous](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/dedreous/32/13849_2.png) [@dedreous](https://discourse.codecombat.com/u/dedreous)\
**Post date:** [January 26, 2020, 11:18am UTC](https://discourse.codecombat.com/t/solved-cant-solve-sarven-shepherd/14966/14 "2020-01-26T11:18:54Z")

</div>

@ albert, notice how your loop statements are stacked:

```python
while True:
    enemies = hero.findEnemies()
    enemyIndex = 0
    
    # Wrap this logic in a while loop to attack all enemies.
    
    while enemyIndex < len(enemies):
        # Find the array's length with: 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, 30)

```

You start with the ‘while true’…let’s call that the parent loop.  
This runs the whole thing.

Then, you embed another while loop, ‘while enemyIndex \< len(enemies’. Let’s call this the ‘child1’ loop.  
This iterates through each enemy found.

You also embed in child1, the if statement, 'if enemy.type != “sand-yak”.  
This acts on those enemies, but only if they are not a sand-yak.

Finally, you have ‘while enemy.health \> 0’.  
This acts on the enemy currently being fought.

Thinking of these levels, where would you want to increment enemyIndex? It would be after attacking an enemy that IS other than a yak, not while attacking it, but before moving to the next enemy. You have the statement in _almost_ the proper spot.

---

<div class="post-metadata">

**Author:** ![albert](https://avatars.discourse-cdn.com/v4/letter/a/48db29/32.png) [@albert](https://discourse.codecombat.com/u/albert)\
**Post date:** [January 28, 2020, 1:02pm UTC](https://discourse.codecombat.com/t/solved-cant-solve-sarven-shepherd/14966/15 "2020-01-28T13:02:57Z")

</div>

Thanks for your response dedreous  
i thought that i have to place enemyIndex += 1 after the attack  
but if the program doesn’t count the yaks that appear i tought it wouldn’t count all other enemies…  
I tried “enemyIndex += 1” after attack inside the “while enemy.health \> 0:” loop, outside of it but inside  
“if enemy.type != “sand-yak”:” after and before the “while enemy.health\>0:” loop  
also outside of them on while true loop but after all the other code…  
i had no problems before placing enemyIndex += 1…

i tried your code and didn’t work

it says code never finished it’s either really slow or has an infinite loop

sorry for my english

---

<div class="post-metadata">

**Author:** ![dedreous](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/dedreous/32/13849_2.png) [@dedreous](https://discourse.codecombat.com/u/dedreous)\
**Post date:** [January 28, 2020, 1:41pm UTC](https://discourse.codecombat.com/t/solved-cant-solve-sarven-shepherd/14966/16 "2020-01-28T13:41:08Z")

</div>

Your enemyIndex += 1 should be the closing statement of the while enemyIndex loop, so yes, it does need to be after the attack. As such, it should be placed in line with the while enemyIndex statement, not the other if and while ones. In your code above, you have it as the closing statement of the embedded while enemy.health loop.

Your English is just fine 😉

---

<div class="post-metadata">

**Author:** ![albert](https://avatars.discourse-cdn.com/v4/letter/a/48db29/32.png) [@albert](https://discourse.codecombat.com/u/albert)\
**Post date:** [February 2, 2020, 11:05pm UTC](https://discourse.codecombat.com/t/solved-cant-solve-sarven-shepherd/14966/17 "2020-02-02T23:05:37Z")

</div>

Hi this level is becoming a nightmare 🤪 🤪 🤪 🤪

There is my new code:

```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
    while enemyIndex < len(enemies):
        # Find the array's length with: 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
    # Between waves, move back to the center.
    hero.moveXY(40, 30)
    pass

```

The hero just walks to the 40,30 point

I feel that I should program something to count the yak that is in the screen, when I see the program I think that the logic of it doesn’t allow the program to restart the while true loop.  
Like if the program blocked on it, so the program can continue reading the new “enemies” that come on the screen… but of course I am not a programer.

Thanks for your time guys,…

---

<div class="post-metadata">

**Author:** ![dedreous](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/dedreous/32/13849_2.png) [@dedreous](https://discourse.codecombat.com/u/dedreous)\
**Post date:** [February 3, 2020, 12:00am UTC](https://discourse.codecombat.com/t/solved-cant-solve-sarven-shepherd/14966/19 "2020-02-03T00:00:22Z")

</div>

@albert: Well, your enemyIndex is in the right spot now, but you still have ‘enemy.health \< 0’…you want to keep attacking if enemy.health is more than zero.

---

<div class="post-metadata">

**Author:** ![albert](https://avatars.discourse-cdn.com/v4/letter/a/48db29/32.png) [@albert](https://discourse.codecombat.com/u/albert)\
**Post date:** [February 4, 2020, 7:43pm UTC](https://discourse.codecombat.com/t/solved-cant-solve-sarven-shepherd/14966/20 "2020-02-04T19:43:31Z")

</div>

🙃 🙃 🙃 🙃 🙃 🙃 🙃 🙃 🙃  
I have no merit because I didn’t solve it but I learnt a lot…  
thank you guys so much  
really thank you all

---

<div class="post-metadata">

**Author:** ![CodingGeek14](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/codinggeek14/32/10566_2.png) [@CodingGeek14](https://discourse.codecombat.com/u/CodingGeek14)\
**Post date:** [March 23, 2020, 1:55pm UTC](https://discourse.codecombat.com/t/solved-cant-solve-sarven-shepherd/14966/21 "2020-03-23T13:55:19Z")

</div>

Hi again, @dedreous.  
I am stuck on Sarven Shepherd.  
Here’s my code:

```python
# Use while loops to pick out the ogre
while True:
    enemies = hero.findEnemies()
    enemyIndex = 0
    if 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
    enemyIndex += 1
# Between waves, move back to the center.
hero.moveXY(40, 32)

```

My hero is standing still.  
Looked at other forums similar to this one (inclusive of this one) and added the pieces of code necessary and still my hero is stationary. Can you edit my code and explain how to beat this level?

[Next page](https://discourse.codecombat.com/t/solved-cant-solve-sarven-shepherd/14966.md?page=2)
