Help with Sarven Shepherd (python) Solved


#1

I can not figure out this infinite loop problem please help
while True:
enemies = hero.findEnemies()
enemyIndex = 1
while enemyIndex < len(enemies):
enemy = enemies[enemyIndex]
if enemy.type != “sand-yak”:
hero.attack(enemy)
if enemy.health > 0:
hero.attack(enemy)
else:
enemyIndex += 1
pass
hero.moveXY(40, 32)


#2

I can’t see the format of your code because you didn’t post it properly but there are some discernible errors.

First is the assignment of enemyIndex = 1. enemyIndex should start at 0 and then increment by 1.
Second, the first hero.attack should be after checking enemy.health and the second hero.attack isn’t necessary.

You will have to post it correctly in order for anyone to see the structure. Please post correctly from now on. It’s really easy and helps us help you. Thanks.

Button01


#3

Maybe this helps out:

while True:
enemies = hero.findEnemies()
enemyIndex = 1
while enemyIndex < len(enemies):
enemy = enemies[enemyIndex]
if enemy.type != “sand-yak”:
hero.attack(enemy)
if enemy.health > 0:
hero.attack(enemy)
else:
enemyIndex += 1
pass
hero.moveXY(40, 32)

#4

That’s better. Now we can see your code and help out


#5

Well, you know how to do it now. But you copied it from here, not the game. It has to be copied from the game in order to have the formatting. When copying it from this board it has no formatting and there’s no indentations. You’re getting closer.

Also, in the previous post I discussed starting your enemyIndex at 0, not 1. You are starting your index at the second object in the array. Python starts counting at 0, not 1.

Three objects in an array. They are numbered, 0, 1, 2.

The first hero.attack should be after checking enemy.health and the second hero.attack isn’t necessary.

The else statement should be deleted.


#6

when I put it at zero it will not attack anything what should I do


#7

You have to post your code - copied from the game to here - in order for us to see your structure. If you’ve made the changes detailed above and the structure is correct, it will work. When you click on the button indicated above, it will tell you where to paste your code. It’s really easy.


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

#9

Thank you for posting your code correctly. However, you didn’t make any of the three changes that I told you need to be fixed. :thinking:


#10

there is still an infinite loop

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


#11

The line to increment enemyIndex += 1 is tabbed over too far. It should line up under the if enemy.type conditional statement.


#12

thank you for helping me with the infinite loop problem


#13

Can someone help me

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

     # Wrap this logic in a while loop to attack all enemies.
    # Find the array's length with:  len(enemies)
while True:
    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(40, 33)

hero won’t move


#14

never mind figured it out