Play Home Contribute GitHub Join Us Discourse Staff Members GitHub Contribution Guides Team

I need help with sarvern shepherd


It keeps on having infinite loops


Is it giving you an infinite loop error? Can you elaborate more on the problem?

As vague as your question is right now, the only advice I can offer is that you should check your while loop’s condition. Make sure you increase the index value by one each time you go through the loop.

Code like this will result in an infinite loop:

index = 0
while index < len(someList):
    someValue = someList[index]
    if someValue > 0:
        index += 1           # index gets incremented
    else:                    # when someValue is not greater than 0
        doAnotherThing()     # then index does not get incremented
                             # and someValue = someList(index) will be the same value
                             # so, when the while loops again
                             # and will fail the same if statement again
                             # (and come back to this else block... again and again...)

How to fix it:

index = 0
while index < len(someList):
    someValue = someList[index]
    if someValue > 0:
        doSomething()    #note: removed the line: index += 1
    # increment the index only at the last line of a while loop,
    # and outside of any nested code-blocks, like this:
    index += 1

On later levels, you’ll learn how to use for loops, which are much better for going through elements in an array.

Help i cant do a lvl

it doesn’t work and yes, it is giving me an infinite loop error


Also it would help if you post your code. With formatting according to the FAQ


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 > 0:
        while <= 0:
            enemyIndex += 1
# Between waves, move back to the center.
hero.moveXY(40, 32)

Here it is in python although it did stop giving me errors. And I have no other options for heroes because i don’t want to subscribe.


Make sure you read my post earlier in this thread.
The comments are important, so read them all.
Compare the differences between the two pieces of code.
You’ll notice that the only difference is the relocation of index += 1.


@DarkLord In your while loop you did

while enemyIndex > len(enemies):

this means the code will never run so it should be

while enemyIndex < len(enemies):