Sarven Sheperd help (Python)

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

What is wrong with this code i have been stuck for hours?

while True:
enemies = hero.findEnemies()
enemyIndex = 0

while enemyIndex > len(enemies):
enemy = enemies[enemyIndex]
if enemy.type != “sand-yak”:
while enemy.health > 0:
hero.attack(enemy)
enemyIndex +=1
pass

hero.moveXY(x, y)

lvl sarven sheperd
i am not asking for an answer but can someone give me a hint?

When you start, enemyIndex is 0, so enemyIndex won’t be greater than len(enemies).

I don’t get that i started with enemyIndex 0

Also, could you please format your code like this?

Hi @TEO Welcome to the discourse :partying_face: :partying_face:
I formatted the for you but next time pls formate the code like the link shown here

while True:
enemies = hero.findEnemies()
enemyIndex = 0

while enemyIndex > len(enemies):
enemy = enemies[enemyIndex]
if enemy.type != “sand-yak”:
while enemy.health > 0:
hero.attack(enemy)
enemyIndex +=1
pass

hero.moveXY(x, y)

Your indentation is wrong, after while and if loop you have to put 4 spaces for just press tab.
Hope it helps :grinning:

Since enemyIndex is less than len(enemies), enemyIndex will never increment and you will have a code that does nothing.

Yupi got it I am in clouddrip now

@TEO is doing it correct, you compare enemyIndex and the length of the enemies.

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