# Sarven shepherd help plz

**URL:** https://discourse.codecombat.com/t/sarven-shepherd-help-plz/33246
**Category:** Level Help
**Created:** [June 27, 2022, 4:44pm UTC](https://discourse.codecombat.com/t/sarven-shepherd-help-plz/33246 "2022-06-27T16:44:37Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![James\_Pfiffner](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/james_pfiffner/32/22991_2.png) [@James\_Pfiffner](https://discourse.codecombat.com/u/James_Pfiffner)
#### Post date: [June 27, 2022, 4:44pm UTC](https://discourse.codecombat.com/t/sarven-shepherd-help-plz/33246/1 "2022-06-27T16:44:37Z")

</div>

help

```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)
        enemyIndex += 1
        pass

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

```

---

<div class="post-metadata">

### Author: ![phoenixRider2022](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/phoenixrider2022/32/29463_2.png) [@phoenixRider2022](https://discourse.codecombat.com/u/phoenixRider2022)
#### Post date: [June 27, 2022, 4:48pm UTC](https://discourse.codecombat.com/t/sarven-shepherd-help-plz/33246/2 "2022-06-27T16:48:35Z")

</div>

I suggest you change out the

> [@James\_Pfiffner](#):
>
> `while enemyIndex < len(enemies):`

to

```python
for enemy in enemies

```

---

<div class="post-metadata">

### Author: ![Aya](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/aya/32/42680_2.png) [@Aya](https://discourse.codecombat.com/u/Aya)
#### Post date: [June 27, 2022, 5:27pm UTC](https://discourse.codecombat.com/t/sarven-shepherd-help-plz/33246/3 "2022-06-27T17:27:47Z")

</div>

I dont recall the for loop being in the desert. At that level while loops are used.

You should backspace the one below to be at the end of the second while loop and not in the if condition

> [@James\_Pfiffner](#):
>
> `enemyIndex += 1`

---

<div class="post-metadata">

### Author: ![Aya](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/aya/32/42680_2.png) [@Aya](https://discourse.codecombat.com/u/Aya)
#### Post date: [June 27, 2022, 5:29pm UTC](https://discourse.codecombat.com/t/sarven-shepherd-help-plz/33246/4 "2022-06-27T17:29:13Z")

</div>

And you should also indent all the code from the second while loop to make it inside the first one

---

<div class="post-metadata">

### Author: ![riticmaster908](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/riticmaster908/32/43503_2.png) [@riticmaster908](https://discourse.codecombat.com/u/riticmaster908)
#### Post date: [June 27, 2022, 10:50pm UTC](https://discourse.codecombat.com/t/sarven-shepherd-help-plz/33246/5 "2022-06-27T22:50:23Z")

</div>

yea Aya is right, the only time u use for loops is probably Shine Getter I think…

---

<div class="post-metadata">

### Author: ![FrostByte](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/frostbyte/32/29050_2.png) [@FrostByte](https://discourse.codecombat.com/u/FrostByte)
#### Post date: [June 28, 2022, 12:08am UTC](https://discourse.codecombat.com/t/sarven-shepherd-help-plz/33246/6 "2022-06-28T00:08:41Z")

</div>

And it should probably be after the `pass` statement, otherwise the loop will break before moving back to the center.

---

<div class="post-metadata">

### Author: ![James\_Pfiffner](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/james_pfiffner/32/22991_2.png) [@James\_Pfiffner](https://discourse.codecombat.com/u/James_Pfiffner)
#### Post date: [June 28, 2022, 4:10pm UTC](https://discourse.codecombat.com/t/sarven-shepherd-help-plz/33246/7 "2022-06-28T16:10:20Z")

</div>

like this?

```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)
for enemy in 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)

```

---

<div class="post-metadata">

### Author: ![James\_Pfiffner](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/james_pfiffner/32/22991_2.png) [@James\_Pfiffner](https://discourse.codecombat.com/u/James_Pfiffner)
#### Post date: [June 28, 2022, 4:24pm UTC](https://discourse.codecombat.com/t/sarven-shepherd-help-plz/33246/8 "2022-06-28T16:24:53Z")

</div>

nevermind I got it (20)

---

<div class="post-metadata">

### Author: ![Aya](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/aya/32/42680_2.png) [@Aya](https://discourse.codecombat.com/u/Aya)
#### Post date: [June 29, 2022, 7:36am UTC](https://discourse.codecombat.com/t/sarven-shepherd-help-plz/33246/9 "2022-06-29T07:36:09Z")

</div>

Good job 👍

if you solved the level, please mark the post as a solution for thetopic to close, and if you couldnt, we’ll be more than happy to help 🙂

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/2X/a/a9445509d1a758c41b5ddcddc8c8e20a5dedb67f.png) [@system](https://discourse.codecombat.com/u/system)
#### Post date: [June 29, 2022, 7:36pm UTC](https://discourse.codecombat.com/t/sarven-shepherd-help-plz/33246/10 "2022-06-29T19:36:59Z")

</div>

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.
