[SOLVED]Sarven Shepard level help

I have been stuck on this for awhile.I dont know whats wrong can someone help pls

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

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

hero.moveXY(40, 32)
    

It has to do with indentations. The ‘while enemyIndex’ loop, needs to be indented, so as to make it an inner loop, or a child loop (member), of the outer ‘while true’ loop.

  • Just to explain, the terms ‘inner loop’ and ‘child loop’ are often used interchangeably. They both imply that there is another loop in the hiarchy (above) that they are working for.
  • This other loop is referred to either the ‘parent loop’, or the ‘outer loop’…all of these terms are correct, it’s just a matter of preference, how you learned, etc.

You will also have at least one other issue, but let’s address that, once you get this one fixed…a bit less confusing that way.

Yep, that looks pretty good. And, you fixed the other issue already…enemyIndex += 1 is properly aligned.

Now, think about when you want to move…

My person hits one person then stops with a red x under him?

As shown above, your moveXY statement is aligned with ‘while true’…that means that it is on the same hierarchical level as the while statement…it also means it will never execute. Instead, it needs to be a member of the while loop, not a peer…it needs to be the final statement of the loop.

wait sorry when i was fixing code i deleted a “o” in hero my bad :sweat_smile:

heh…been there myself. Does the level pass now? If it does, please edit the posts above (except the first one) and remove your code. We don’t want to post solutions, as someone may find that post, copy and paste your code and pass the level without having learned a thing.

1 Like

okay thanks for all the help

1 Like

This is my code. It seems fine, but i keep dying
I’m in a class, so i can’t get better armor or weapons

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

Howdy and welcome to the forum!

With this code, you are testing to see that the enemy is not a yak…good! Next, you test to see if its health is greater than zero…ok, but not what you are looking for. I strongly suggest that you don’t delete the comments, as they tend to help guide you and keep you on track:

            # While the enemy's health is greater than 0, attack it!

Oh ok, thanks!
I thought it was because i had not alot of health or something

1 Like