# Sarven Shepard Need help

**URL:** <https://discourse.codecombat.com/t/sarven-shepard-need-help/12247>\
**Category:** Uncategorized\
**Created:** [August 22, 2017, 4:38am UTC](https://discourse.codecombat.com/t/sarven-shepard-need-help/12247 "2017-08-22T04:38:31Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![mbruno150](https://avatars.discourse-cdn.com/v4/letter/m/c0e974/32.png) [@mbruno150](https://discourse.codecombat.com/u/mbruno150)\
**Post date:** [August 22, 2017, 4:38am UTC](https://discourse.codecombat.com/t/sarven-shepard-need-help/12247/1 "2017-08-22T04:38:31Z")

</div>

Can someone please help me!  
Please note that if I start the enemyIndex = 0 as provided by the pre-written code the hero never attacks any enemies. When I change the enemyIndex = 1 the hero attacks the first wave of enemies and then returns to center, but when the next wave of yaks and ogres come the hero never seems to find the next wave of ogres (i.e. enemies). When I watch the game run you can see the hero is not finding the enemy for the second wave. First of all in theory the code should work even if enemyIndex = 0. In fact in python list always start with position zero not one. Since the hero is not finding enemies after he returns that implies that once the hero moves back to center the program ceases at that point, but since the else statement is nested within the while True loop I would think it would loop back up to the top restart from the while True: portion of the code, but clearly that is not happening. Any help, suggestions, corrections would be greatly appreciated. This has been driving me crazy! lol

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

# 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.
    else:
        hero.moveXY(40,32)
```

---

<div class="post-metadata">

**Author:** ![mbruno150](https://avatars.discourse-cdn.com/v4/letter/m/c0e974/32.png) [@mbruno150](https://discourse.codecombat.com/u/mbruno150)\
**Post date:** [August 22, 2017, 4:42am UTC](https://discourse.codecombat.com/t/sarven-shepard-need-help/12247/2 "2017-08-22T04:42:48Z")

</div>

For some reason when I past my code into window it separates the first three lines of code and makes it look like the while enemyIndex \< len(enemies) is not indented or nested within the while true loop. In the original code it is indented.  
Thanks

---

<div class="post-metadata">

**Author:** ![byobcello](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/byobcello/32/6684_2.png) [@byobcello](https://discourse.codecombat.com/u/byobcello)\
**Post date:** [August 22, 2017, 4:55am UTC](https://discourse.codecombat.com/t/sarven-shepard-need-help/12247/3 "2017-08-22T04:55:52Z")

</div>

How should you get of the `while enemyIndex < len(enemies):` loop when the `enemy.type` _is_ “sand-yak”?

---

<div class="post-metadata">

**Author:** ![mbruno150](https://avatars.discourse-cdn.com/v4/letter/m/c0e974/32.png) [@mbruno150](https://discourse.codecombat.com/u/mbruno150)\
**Post date:** [August 22, 2017, 5:02am UTC](https://discourse.codecombat.com/t/sarven-shepard-need-help/12247/4 "2017-08-22T05:02:45Z")

</div>

byobcello  
The code is not if enemy.type is “sand-yak”. If you look above the code is **if enemy-type != “sand-yak”:**  
!= means not equal to. Therefore the if statement states that if enemyIndex \< len(enemy) total amount of enemies found and the enemy.type is not equal to “sand-yak” then start the nested loop below the if statement.  
I hope that makes sense. Thank you.

---

<div class="post-metadata">

**Author:** ![byobcello](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/byobcello/32/6684_2.png) [@byobcello](https://discourse.codecombat.com/u/byobcello)\
**Post date:** [August 22, 2017, 5:05am UTC](https://discourse.codecombat.com/t/sarven-shepard-need-help/12247/5 "2017-08-22T05:05:08Z")

</div>

The `else:` can also be understood as `elif enemy.type == "sand-yak":`

---

<div class="post-metadata">

**Author:** ![mbruno150](https://avatars.discourse-cdn.com/v4/letter/m/c0e974/32.png) [@mbruno150](https://discourse.codecombat.com/u/mbruno150)\
**Post date:** [August 22, 2017, 5:13am UTC](https://discourse.codecombat.com/t/sarven-shepard-need-help/12247/6 "2017-08-22T05:13:31Z")

</div>

Sorry i am not exactly sure what your are suggesting I change… Are you saying the hero.move(x,y) statement not be placed under the else; statement? If so what do you suggest.  
in the hint section it states the move(xy) command should be after(outside) your while enemyIndex loop but inside the main while True loop.

---

<div class="post-metadata">

**Author:** ![byobcello](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/byobcello/32/6684_2.png) [@byobcello](https://discourse.codecombat.com/u/byobcello)\
**Post date:** [August 22, 2017, 5:19am UTC](https://discourse.codecombat.com/t/sarven-shepard-need-help/12247/7 "2017-08-22T05:19:35Z")

</div>

```python
    if enemy.type != "sand-yak":
        while enemy.health > 0:
            hero.attack(enemy)
        enemyIndex += 1
    else: # this line is like: elif enemy.type == "sand-yak"
        hero.moveXY(40,32)

```

Work your way through the while loop starting with an `enemy` that has a `type` of `"sand-yak"`.  
(Also, don’t forget to change `enemyIndex` back to `0` before the `while enemyIndex < len(enemies):` loop)

---

<div class="post-metadata">

**Author:** ![mbruno150](https://avatars.discourse-cdn.com/v4/letter/m/c0e974/32.png) [@mbruno150](https://discourse.codecombat.com/u/mbruno150)\
**Post date:** [August 22, 2017, 5:30am UTC](https://discourse.codecombat.com/t/sarven-shepard-need-help/12247/8 "2017-08-22T05:30:30Z")

</div>

I thought thats what I wanted it do. Another words if the found that the enemies are only “sand yaks” then move back to the center. I definitely understand that my code is not resetting the enemyIndex back to zero, but I am not sure how to do that because I have to add to the enemyIndex in the loop so that it iterates through loop of all enemies in the array that appear on the screen. . the other problem is when I initially set enemyIndex = 0 the hero never attacks the first wave ogres which I don’t understand. Yet if I set it to 1 the hero at least attacks the first wave of enemies.

---

<div class="post-metadata">

**Author:** ![mbruno150](https://avatars.discourse-cdn.com/v4/letter/m/c0e974/32.png) [@mbruno150](https://discourse.codecombat.com/u/mbruno150)\
**Post date:** [August 22, 2017, 5:36am UTC](https://discourse.codecombat.com/t/sarven-shepard-need-help/12247/9 "2017-08-22T05:36:00Z")

</div>

Have you played this level and figured out a solution?

---

<div class="post-metadata">

**Author:** ![byobcello](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/byobcello/32/6684_2.png) [@byobcello](https://discourse.codecombat.com/u/byobcello)\
**Post date:** [August 22, 2017, 5:54am UTC](https://discourse.codecombat.com/t/sarven-shepard-need-help/12247/10 "2017-08-22T05:54:31Z")

</div>

So, let’s start with an `enemyIndex` of 0.  
Assume the first element of your `enemies` list is actually an `enemy` of type `"sand-yak"`.

```python
while enemyIndex < len(enemies)

```

`enemyIndex` is 0, and let’s say the length of `enemies` is `3`.  
So, `enemyIndex < len(enemies)`, and the `while` loop is entered.

```python
enemy = enemies[enemyIndex]

```

The first element of enemies (which is a sand-yak) is now stored in `enemy`.

```python
if enemy.type != "sand-yak"

```

The `enemy.type` is `sand-yak`, so the statement `enemy.type != "sand-yak"` evaluates to `False`.  
Skip to the `else`.

```python
else: # elif enemy.type == "sand-yak":
    hero.moveXY(40,32)

```

All we do now is just have the hero move.  
The code inside the `while` loop ends here, so go back to the start of the `while` loop and run through it a second time.

> **Click here if you need a hint**
>
> Pay special attention to the `enemyIndex`. When does it change? When _should_ it change?
