Sarven Shepard help

Okay, I HATE it when I have to ask for help online but this is just ridiculous. I did exactly what the game asked for me to code and my character just stands there until he dies. There are no errors I just don’t know whats wrong.

Here’s 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 True:
        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)
                if hero.isReady("powerUp"):
                    hero.powerUp()
        pass        
        

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

I think I see your error. while True: is an infinite loop, essentially you have two infinite loops which will never break out. Hence your hero not taking any actions. For your second while statement try having it iterate through the list of enemies you found with enemies = hero.findEnemies(). Your enemies variable is a list of enemies, by taking the len(enemies) which returns the length you can iterate thought. Try haveing the while statment check to see if a iteration variable is = len. At the end of the loop before it exits you can add one to the variable with itrVar =+ 1.
-Cheers

1 Like

Thanks! The reason why I used a while True loop was because the level told me to,[quote=“slamdunx, post:1, topic:7602”]
# Wrap this logic in a while loop to attack all enemies
[/quote]
which made me think that. Thanks for telling me that, in my opinion it should be a bit less specific

It means use a while and you chose what to put after it. Just like an if statement. I agree about the ambiguity, in the end I used a for enemy in enemies: loop. Which I think is easier, but I don’t think it shown at this point.
-Cheers

Well Cryogenic, I’be already figured the level out, thanks for the help!:smile:

1 Like