# Sarven Shepherd help (Python)

**URL:** <https://discourse.codecombat.com/t/sarven-shepherd-help-python/3177>\
**Category:** Uncategorized\
**Created:** [March 20, 2015, 5:50pm UTC](https://discourse.codecombat.com/t/sarven-shepherd-help-python/3177 "2015-03-20T17:50:43Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![armstrong\_peterson](https://avatars.discourse-cdn.com/v4/letter/a/85e7bf/32.png) [@armstrong\_peterson](https://discourse.codecombat.com/u/armstrong_peterson)\
**Post date:** [March 20, 2015, 5:50pm UTC](https://discourse.codecombat.com/t/sarven-shepherd-help-python/3177/1 "2015-03-20T17:50:44Z")

</div>

Whenever I run the code my hero just stands there. Please help me.

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

loop:
    enemies = self.findEnemies()
    enemyIndex = 0

    # Wrap this logic in a while loop to attack all enemies.
    while enemyIndex < 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:
                self.attack(enemy)
                enemyIndex += 1
        pass

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

```

---

<div class="post-metadata">

**Author:** ![J\_F\_B\_M](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/j_f_b_m/32/2756_2.png) [@J\_F\_B\_M](https://discourse.codecombat.com/u/J_F_B_M)\
**Post date:** [March 20, 2015, 11:25pm UTC](https://discourse.codecombat.com/t/sarven-shepherd-help-python/3177/2 "2015-03-20T23:25:26Z")

</div>

Imagine what happens when the enemy at index 0 is a sand-yak. Think trough your program before looking into the spoiler.

> **Spoiler**
>
> Your program looks like this:
> 
> 1. Find `enemies` (let’s say this returns 6 entries, with the first entry being a **Sand-Yak** )
> 2. Set `enemyIndex` to `0`
> 3. As long as 0 is smaller than … _enemies is not a number?!?_  
> Use `len(enemies)` instead.
> 4. (Given the error on line 4 didn’t happen)  
> Get `enemies[enemyIndex]` (which is a **Sand-Yak** )
> 5. If `enemy.type ( == Sand-Yak )` equals `sand-yak` --\> false, jump over if-block
> 6. `pass` -\> does nothing
> 7. move to `(40, 32)`
> 8. End of while, goto command 3.
> 
> As you can see (especially from line 8) you can see that you will never do anything else than moving to `(40, 32)`, as `enemyIndex` never changes.

---

<div class="post-metadata">

**Author:** ![armstrong\_peterson](https://avatars.discourse-cdn.com/v4/letter/a/85e7bf/32.png) [@armstrong\_peterson](https://discourse.codecombat.com/u/armstrong_peterson)\
**Post date:** [March 21, 2015, 3:16am UTC](https://discourse.codecombat.com/t/sarven-shepherd-help-python/3177/3 "2015-03-21T03:16:19Z")

</div>

Thanks a ton. I’ve been having trouble with while loops and now I see my error. Yet I still can’t get it right. when I run my updated code my hero kills two ogres then moves back to 40, 32. Then the “While enemyIndex \< len(enemies)” I get a hard execution limit error. What am I doing wrong?

# Use while loops to pick out the ogre

loop:  
enemies = self.findEnemies()  
enemyIndex = 1

# Wrap this logic in a while loop to attack all 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!
        self.attack(enemy)
        enemyIndex += 1
    

# Between waves, move back to the center.

self.moveXY(40,32)
```

---

<div class="post-metadata">

**Author:** ![nick](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/nick/32/784_2.png) [@nick](https://discourse.codecombat.com/u/nick)\
**Post date:** [March 21, 2015, 4:59am UTC](https://discourse.codecombat.com/t/sarven-shepherd-help-python/3177/4 "2015-03-21T04:59:07Z")

</div>

Try incrementing enemyIndex regardless of whether the enemy is a yak or not.

---

<div class="post-metadata">

**Author:** ![Chris\_Bishop](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/chris_bishop/32/5230_2.png) [@Chris\_Bishop](https://discourse.codecombat.com/u/Chris_Bishop)\
**Post date:** [November 25, 2016, 5:57pm UTC](https://discourse.codecombat.com/t/sarven-shepherd-help-python/3177/5 "2016-11-25T17:57:15Z")

</div>

I breezed through the first initial 300 levels, it was simple enough, but on this level a slew of new definitions and functions come about and I am completely lost, been stuck for three weeks I really need help, I’ve tried absolutely everything.

---

<div class="post-metadata">

**Author:** ![Nicholas\_Horton](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/nicholas_horton/32/5621_2.png) [@Nicholas\_Horton](https://discourse.codecombat.com/u/Nicholas_Horton)\
**Post date:** [January 20, 2017, 5:14am UTC](https://discourse.codecombat.com/t/sarven-shepherd-help-python/3177/6 "2017-01-20T05:14:38Z")

</div>

The “while True” function overrides everything as long as your main value is true, so everything else has to be within it. That was the problem I had, at least.

---

<div class="post-metadata">

**Author:** ![Elie\_Juton\_elie2349](https://avatars.discourse-cdn.com/v4/letter/e/d2c977/32.png) [@Elie\_Juton\_elie2349](https://discourse.codecombat.com/u/Elie_Juton_elie2349)\
**Post date:** [June 22, 2017, 10:37am UTC](https://discourse.codecombat.com/t/sarven-shepherd-help-python/3177/7 "2017-06-22T10:37:57Z")

</div>

Just write this :

Edited:  
`<snip>` we don’t get correct code

---

<div class="post-metadata">

**Author:** ![Gopeman](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/gopeman/32/7724_2.png) [@Gopeman](https://discourse.codecombat.com/u/Gopeman)\
**Post date:** [April 6, 2018, 8:11am UTC](https://discourse.codecombat.com/t/sarven-shepherd-help-python/3177/8 "2018-04-06T08:11:25Z")

</div>

help, this is my code:  
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)  
enemyIndex += 1  
pass

```
# Between waves, move back to the center.
hero.moveXY(40, 32)
it says that i need a : after while enemy health > 0:
```

---

<div class="post-metadata">

**Author:** ![Gabriel\_Dupuis](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/gabriel_dupuis/32/7450_2.png) [@Gabriel\_Dupuis](https://discourse.codecombat.com/u/Gabriel_Dupuis)\
**Post date:** [April 6, 2018, 10:00am UTC](https://discourse.codecombat.com/t/sarven-shepherd-help-python/3177/9 "2018-04-06T10:00:51Z")

</div>

can you post you code between the dots using \</\>  
It is very hard to see clear without that since there is a lot of incrementation in this level
