Sarven Shepard -- Python

ok
i reset because i thought that was a good idea
I HAVE THE SAME PROBLEM

# 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!
        enemyIndex += 1
        hero.attack(enemy)
        pass

# Between waves, move back to the center.
hero.moveXY(40, 32)

Ok iā€™ve been stuck on this level for three days can someone help with my current code here

Your enemyIndex is in the wrong indentation. Right now, it gets stuck when the enemy.type == ā€œsand-yakā€ because you only increase the enemyIndex if it isnā€™t a sand-yak. You need to increase the enemyIndex every time you go through the while loop so it needs to be on that indentation.

What @brooksy125 says. Also look at your indentation all the way down. Everything needs to be inside the while True loop, including the second while statement and the hero.moveXY statement. Otherwise it wonā€™t happen!

Jenny

1 Like

Just tried that AND I HAVE THE EXACT SAME PROBLEM

# 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!
        hero.attack(enemy)
        pass
    if enemy.health == "0":
        enemyIndex += 1
    hero.moveXY(40, 32)

You still donā€™t have the enemyIndex += 1 in the correct indentation.

while enemyIndex < len(enemies):
    # other code
    enemyIndex += 1

And @jka2706 caught a bigger error, you didnā€™t have your second while loop in the while True: loop which means you are only running the first three lines of your code. Everything in the second while loop needs to be indented one more time to be in the while True: loop.

2 Likes

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