Sarven Shepherd "infinite loop"?

My code in the level Sarven Shepherd looks correct, but it says “Code never finished. It’s either really slow or has a infinite loop.”. 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)
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
# Between waves, move back to the center.
hero.moveXY(40,32)

Please help me. Thanks

your code looks correct but since it wasn’t formatted properly for this forum, I can’t see the structure. Since the code looks correct, it’s probably a structure issue. Try reading this link on how to paste code here. It’s really simple and basically just involves using the </> button.

https://discourse.codecombat.com/faq

Be sure to copy and paste it from the game - not this forum - so that the formatting is retained.

Sorry about that here’s the structure:
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
    # Between waves, move back to the center.
    hero.moveXY(40,32)
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
    # Between waves, move back to the center.
    hero.moveXY(40,32)

I found my mistake… sorry…

1 Like

yup, it was a structure issue. :grinning: