Sarven Shepard/ Python/ Help[SOLVED]

My character isn’t moving or attacking

# 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)
    length = 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)
        pass
    else:
        # Between waves, move back to the center.
        hero.moveXY(40, 32)

Use a while loop (20 chars)

Where should I place the loop and what should be in it

You should wrap

    length = 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)
        pass
    else:
        # Between waves, move back to the center.
        hero.moveXY(40, 32)

in

so there should be two while loops?

Yes, one for looping through enemies TO ATTACK and one for ATTACK.

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 True:
    length = 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)
        pass
    else:
        # Between waves, move back to the center.
        hero.moveXY(40, 32)
    

Um no, it will be a while loop and not a while true loop.

What do I put in the while loop?
Im sorry if im missing something obvious

Try taking out your second while true loop, @Some_Guy .
I have a four-line code, so you may be making things over complicated.

No he isn’t. If your code’s right in’ts not supposed to be 4 lines.

I have updated my code a bit so i will edit the main code

I updated the top code and it still has the issue

I’m replying to your first post following the logic of your program. Main problem is the indentation and you must increment the enemyIndex. So after length = len(enemies)

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

all this in the main while True loop. You were very close :slight_smile:

2 Likes

Oh my god thank you so much : D

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