Lurkers help please!


#1
#I can't do it! I just stay in one place, and it says that the hard execution limit of 3000000 has been exceeded!

# findEnemies returns a list of all your enemies.
# Only attack shamans. Don't attack yaks!

enemies = self.findEnemies()
enemyIndex = 0
enIndex = 1
enemyI = 2
# Wrap this section in a while loop to iterate over all enemies.
# While the enemyIndex is less than the length of enemies
while enemyIndex < enemies.length:
    enemy = enemies[enemyIndex]
    if enemy.type == 'shaman':
        while enemy.health > 0:
            self.attack(enemy)
    enemy = enemies[enIndex]
    if enemy.type == 'shaman':
        while enemy.health > 0:
            self.attack(enemy)
    enemy = enemies[enemyI]
    if enemy.type == 'shaman':
        while enemy.health > 0:
            self.attack(enemy)
# Remember to increment enemyIndex

#2

Hello, SuperTux, and welcome. Please read the FAQ before you post again, so you can learn how to format your code properly. I’ve done it for you this time.

The error message “Hard execution limit of 3000000 exceeded” tells you that there is an infinite loop. This is caused by the while-loop you have used. The length of self.findEnemies(), in this level, is 6, since there are three shamans and three sand yaks. However, since enemyIndex is 0, and you never increases it, the statement while enemyIndex < enemies.length: evaluates to while True:, an infinite loop. Here is how you can fix your problem:

Get rid of all your variables except enemies and enemyIndex. Next, think about your problem this way:

Use your while-loop to iterate over all enemies. You’ve already done this. Next, check if the enemy you are examining is a shaman. If it is, attack it until it dies. If it isn’t, move on. Either way, the final step is to “increment” enemyIndex, or increase it by one. This causes you to move on the the next enemy in the list.


#3

Okay, I think I did what you asked, but Tharin attacks yaks! (I removed the second “=” in the if enemy.type statements as well)

enemies = self.findEnemies()
enemyIndex = 0
while enemyIndex < enemies.length:
    enemy = enemies[enemyIndex]
    if enemy.type = "shaman":
        while enemy.health > 0:
            self.attack(enemy)
            if enemy.health < 0:
                enemyIndex = enemyIndex + 1
    if enemy.type = "shaman":
        while enemy.health > 0:
            self.attack(enemy)
            if enemy.health < 0:
                enemyIndex = enemyIndex + 1
    if enemy.type = "shaman":
        while enemy.health > 0:
            self.attack(enemy)
            if enemy.health < 0:
                enemyIndex = enemyIndex + 1
    break