# Sarven Shepherd (python)

**URL:** https://discourse.codecombat.com/t/sarven-shepherd-python/29348
**Category:** Level Help
**Created:** [June 17, 2021, 2:25am UTC](https://discourse.codecombat.com/t/sarven-shepherd-python/29348 "2021-06-17T02:25:23Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![enPointe77](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/enpointe77/32/43632_2.png) [@enPointe77](https://discourse.codecombat.com/u/enPointe77)
#### Post date: [June 17, 2021, 2:25am UTC](https://discourse.codecombat.com/t/sarven-shepherd-python/29348/1 "2021-06-17T02:25:24Z")

</div>

Hi, sorry for making another topic, (I’ve been making a lot recently), but I can’t figure out what’s going on with my code and why it’s not working, can someone help please?

```python
return #Commented out to stop infinite loop.
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.
    # 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 > 0:
                hero.attack(enemy)
        pass
    
    # Between waves, move back to the center.
    hero.moveXY(40, 32)
    

```

@Chaboi_3000?

---

<div class="post-metadata">

### Author: ![Chaboi\_3000](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/chaboi_3000/32/48486_2.png) [@Chaboi\_3000](https://discourse.codecombat.com/u/Chaboi_3000)
#### Post date: [June 17, 2021, 2:33am UTC](https://discourse.codecombat.com/t/sarven-shepherd-python/29348/2 "2021-06-17T02:33:21Z")

</div>

Probably because your code is commented? 😉 Try removing the `return` statements.

---

<div class="post-metadata">

### Author: ![enPointe77](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/enpointe77/32/43632_2.png) [@enPointe77](https://discourse.codecombat.com/u/enPointe77)
#### Post date: [June 17, 2021, 2:34am UTC](https://discourse.codecombat.com/t/sarven-shepherd-python/29348/3 "2021-06-17T02:34:57Z")

</div>

But then it won’t load again! AHHHHH!

---

<div class="post-metadata">

### Author: ![Chaboi\_3000](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/chaboi_3000/32/48486_2.png) [@Chaboi\_3000](https://discourse.codecombat.com/u/Chaboi_3000)
#### Post date: [June 17, 2021, 2:36am UTC](https://discourse.codecombat.com/t/sarven-shepherd-python/29348/4 "2021-06-17T02:36:53Z")

</div>

You forgot to add to the enemy index, so it’s stuck forever going through the array.

---

<div class="post-metadata">

### Author: ![enPointe77](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/enpointe77/32/43632_2.png) [@enPointe77](https://discourse.codecombat.com/u/enPointe77)
#### Post date: [June 17, 2021, 2:40am UTC](https://discourse.codecombat.com/t/sarven-shepherd-python/29348/6 "2021-06-17T02:40:16Z")

</div>

Here’s my code and I’m still standing still.

```python
#Commented out to stop infinite loop.
#Commented out to stop infinite loop.
#Commented out to stop infinite loop.
#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.
    # 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 > 0:
                hero.attack(enemy)
        enemyIndex += 1
    pass
    
    # Between waves, move back to the center.
    hero.moveXY(40, 32)
    

```

---

<div class="post-metadata">

### Author: ![enPointe77](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/enpointe77/32/43632_2.png) [@enPointe77](https://discourse.codecombat.com/u/enPointe77)
#### Post date: [June 17, 2021, 2:41am UTC](https://discourse.codecombat.com/t/sarven-shepherd-python/29348/7 "2021-06-17T02:41:13Z")

</div>

I’m not moving and I’m not fighting.

---

<div class="post-metadata">

### Author: ![RangerGrant9307](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/rangergrant9307/32/26001_2.png) [@RangerGrant9307](https://discourse.codecombat.com/u/RangerGrant9307)
#### Post date: [June 17, 2021, 1:21pm UTC](https://discourse.codecombat.com/t/sarven-shepherd-python/29348/8 "2021-06-17T13:21:33Z")

</div>

This is a little advanced, but try a for loop instead.

```python
while True:
    enemies = hero.findEnemies()
    targets = []
    for enemy in enemies:
        if enemy.type <> 'sand-yak'
            targets.append(enemy)
            #do something

```

Hope this helps!  
Append for loops aren’t that hard lol  
-Grant

---

<div class="post-metadata">

### Author: ![Eric\_Tang](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/eric_tang/32/15310_2.png) [@Eric\_Tang](https://discourse.codecombat.com/u/Eric_Tang)
#### Post date: [June 17, 2021, 1:33pm UTC](https://discourse.codecombat.com/t/sarven-shepherd-python/29348/9 "2021-06-17T13:33:32Z")

</div>

I think you shouldnt do for loops yet if your not there yet.

---

<div class="post-metadata">

### Author: ![RangerGrant9307](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/rangergrant9307/32/26001_2.png) [@RangerGrant9307](https://discourse.codecombat.com/u/RangerGrant9307)
#### Post date: [June 17, 2021, 1:34pm UTC](https://discourse.codecombat.com/t/sarven-shepherd-python/29348/10 "2021-06-17T13:34:28Z")

</div>

Hey I’m just trying to help ok? 🙂

---

<div class="post-metadata">

### Author: ![enPointe77](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/enpointe77/32/43632_2.png) [@enPointe77](https://discourse.codecombat.com/u/enPointe77)
#### Post date: [June 17, 2021, 10:11pm UTC](https://discourse.codecombat.com/t/sarven-shepherd-python/29348/11 "2021-06-17T22:11:19Z")

</div>

@CocoCharlie helped me off of the discourse, but thanks.
