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

[SOLVED] Help on Sarven Sheperd

My hero just sits there without doing anything

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 < 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(40, 32)
    

This line is currently a member of the ‘while enemy.health’ loop. This means that it will keep incrementing on every iteration of that loop. Instead, move it so that it is the final statement of the outer ‘while enemyIndex’ loop.

Also, ‘!= 0’ is not the same thing as ‘>= 0’…

1 Like

My hero still doesn’t move :frowning_face:

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 < 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(40, 32)

enemyIndex is still counting up (incrementing) every time that while loop runs…you need to ‘un’-indent it two times.

1 Like

Like this?


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 < 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(40, 32)

Try to put this inside the while true loop.(by putting 4 spaces before each of those lines)

Andrei

Close…enemyIndex is now in the correct spot. However, you also moved the whole ‘while enemyIndex’ block too…that needs to be indented.

1 Like

:sweat: They still don’t move

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 < 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(40, 32)

You need to compare enemyIndex to the length of the enemies array…the comment gives you a hint.

3 Likes

When I run it it says that the code has an infinite loop?

Share your current code please.

1 Like

Mod edit: Solution removed

Oops, I may have missed this the other day…sorry if I did.

Indent ‘enemyIndex += 1’ one more time.

This will put in line with the ‘while enemyIndex’ loop, making it a member of that loop. Right now, it is a member (even with) the ‘while True’ loop.

2 Likes

Thanks, it worked. :grinning:

1 Like

Woohoo! Glad you got it solved :slight_smile:

1 Like

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