Sarven Desert Sarven Shepherd

Could someone explain to me what’s wrong with my code? My hero stays put and does not attack ogres. I can’t figure why…

# 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 > 0:
    enemyIndex += 1
    # Between waves, move back to the center.
    hero.moveXY(40, 32)

You should indent this line and what’s after it once

and this should be outside the while True loop (before it)

I’m curious about putting this before the while True loop:

enemies = hero.findEnemies()

I did this level myself just now with both of those still within the while True loop, and it worked (provided all of the following code was within that same big while-True loop - requiring the indentation you recommended).

It seems to me that having both these lines within the while True loop would be necessary, since new enemies showed up periodically and would not have been indexed in an array run once at the very beginning of the level.

I’m just now starting to understand arrays, so I could be mistaken, or there could be more ways to do this. Would you please explain more?

Thank you. I’ll try it to see if it works.

This line should stay in the while True loop like as you said as there are always enemies that are coming. As for the line after it, I believe you are right, it should also stay inside. So possibly the only problem was the indentation. Thank you for correcting me.

1 Like

And thanks to you for clarifying. :slight_smile:

I had actually been really frustrated in earlier levels not knowing how to use an array (but knowing I wanted one). I finally got there, but wasn’t at all sure I totally understood all the nuance. I really appreciate you helping and providing a response.

1 Like