"Lurkers" level question

I’m curious about why something doesn’t work.

Below is the code that did work. My question is why isn’t “while enemyIndex < len(enemies):” substitutable with “while enemyIndex < 3”?. There are 6 enemies so from my understanding after the third shaman is killed enemyIndex would = 3 and len(enemies)=3, which is why the first code works. By this logic “enemyIndex < 3” should be equal to "enemyIndex < len(enemies). My character just stands still instead of entering the loop.

enemyIndex = 0

# Wrap this section in a while loop to iterate over all enemies.
enemy = enemies[enemyIndex]
while enemyIndex < len(enemies):
    enemy = enemies[enemyIndex]
    if enemy.type == 'shaman':
        while enemy.health > 0:
            self.attack(enemy)
    enemyIndex += 1

Once you enter the while-loop, len(enemies) is a set value. In this case, it is 6. It will always be considered 6 until the while-loop is over. Also, you are assuming that all the shamans will be first on the list enemies. It is quite possible that the yaks are the first three elements of the list, and so while enemyIndex < 3 would run through all the yaks and stop.

1 Like

Oh everything makes sense now! Thank you. As a suggestion for the game I feel like there should be a smoother introduction for arrays, or a wiki where I can check what the definitions (the tips menu just tells me when to use them).

Try replaying Sarven Savior. If you did it correctly, it should give you a goos introduction already.