Play Home Contribute Github Join Us! Discourse Staff Members Github Contribution Guides Team

Sarven Sheperd help (Python)

Hey,

I need some help with the level “Sarven Shepard”. My hero stands still and won’t do anything at all. Here is my code:

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)
    
        
    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)
        pass

    # Between waves, move back to the center.
hero.moveXY(x, y)

Thanks!

Ok, right now your code has only identified one enemy
enemy = enemies[enemyIndex]
which is the correct approach. You are just missing a few other pieces That enemy is likely a “sand-yak”, so it won’t attack and does nothing. I’ll try to explain the concept with more direction, but won’t actually show you code yet.

To find other enemies, you need to increase your enemyIndex to step through the enemies array. You do this in a while loop with a condition. In this case, the condition should be while enemyIndex is less than the length (or count) of enemies. Add this bit of code just before your enemy variable.

One last piece, don’t forget to increase your index after you run through the code to attack.

Ok. I did that, and it came up with the error saying my loop is either very slow, or an infinite loop.

Did you increase your enemyIndex +=1 to walk through the entire enemies array? It also has to be in the correct spot. You don’t want it on the same indentation of the attack, because if you don’t attack, it won’t increase.

Or the other likely spot enemyIndex < len(enemies)?

If it isn’t one of those two spots, I’ll have to see your updated code, please.

I made sure of both of those, so here is my updated code:

# 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(x, y)
    

While your index is not indented with the attack, it is still after you check the enemy type. If that first enemy is a sand-yak it won’t run the rest of the code, and won’t add one to the index. I typically add the index as the first thing after I create the while loop and then add the rest of the code in the middle to make sure the index is there and on the correct indentation.

I believe the developers intentionally made the first enemy a sand -yak just so you run into this challenge. :crazy_face:

so what you are saying is I should put the

enemyIndex += 1

to right after the while enemyIndex loop?

It needs to be in the enemyIndex loop but after the rest of the if statement code. Move it down below the pass and move the indentation back to be on the same indentation as the if statement.

This way, regardless on whether the if statement is true or false, you will index by one.

while ...:
    enemy...
    if ...
     #the rest of your if statement
    enemyIndex += 1

But if i do that, I think (?) I will just be putting it where it was originally.

Not quite, the last one was still within the if statement.

    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  # not here because it is in the if statement - if sand - yak no  index
            pass
        # should be here
2 Likes

I got it! thanks so much!

You are very Welcome!! :nerd_face:

I still forget to add the index at the end sometimes. When I can remember, I try to add the index as soon as I start the while condition just so it is there and in the right indentation.

while...
    # then add all the extra code in the middle.
    index +=1