# [Desert] Sarven Shepherd - Hard execution limit of 3000000 exceeded

Hi o/

I’m blocked in this level
My character just stand, do nothing else.

``````# 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 < 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:
self.attack(enemy)
enemyIndex += 1
break
# Between waves, move back to the center.
self.moveXY(39, 32)``````

Are you sure about the position of the line `enemyIndex += 1`?
With this code, you are increasing `enemyIndex` after each attack to `enemy` if it is not a sand-yak. Just after `enemy` is dead, `break` makes you exit the `while` loop.
If you find a sand-yak, again, you `break` the first `while` loop and in any case you do not check all the `enemies`.

Can you correct my code so i can see ?

For the enemyIndex += 1, i don’t really see how putting him. I just assume i put on for every ennemies. I mean, it’s what i though.

So, i have for the yak, i have to put something like:

if enemy:
if enemy.type != “sand-yak”:

Could this work ? Because i check if there enemy, and if it’s not a yak.

You don’t need to use a double `if`, you can use the `and` operator to connect

``````if enemy and enemy.type != "sand-yak":
``````

In this way if there is an `enemy`, you check if the type is correct.

Then, you have just to remove the `break` and move `enemyIndex += 1`:

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

In this way, if you find a sand-yak nothing happens, but if you find an ogre you attack until it’s died and then you go the following element of `enemies`.

1 Like

In the game, we don’t really learn these thing, we just have a level we they are showed, and after, we have to use it directly.
I hope some others chapter won’t be learned that way, because it’s pretty hard.

This forum is part of the learning process

Ok

Next school years, i’ll be in a formation about web programming.
So i’ll ear a lot of HTML/CSS, a little part of Java/Javascript, and some server based langage.

(I know Python have nothing to do with, but i planned to learned some others like Python (and i like Python because it’s not really hard)

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

``````# 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!
while enemy.health > 0:
self.attack(enemy)
enemyIndex += 1

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

The code above has been corrected, but I would suggest you think about a couple of things as it is not a great answer unless you are over geared for the level.

1. Is there a better order in which to attack the mobs?
Currently, you could end up zigzagging all over the field. Do you have access to findNearest(), the health property and/or distanceTo() for example?
2. When your targets are outside of your current range, in which direction should you move or use moveXY?