Problem with infinite loop - Python


#1

Hello, i got an infinite-loop-error but i cant find it…

in this code i want to attack the archers before i attack the other enemies.

    enemiesIndex = 0
    loopCounts = 0
    
    loop:
        enemies = self.findEnemies()

        while enemiesIndex <= len(enemies):
            enemy = enemies[enemiesIndex]

            if loopCounts < 1:
                if enemy.type == "archer":
                    while enemy.health > 0:
                        self.attack(enemy)
            elif loopCounts >= 1:
                if enemy.type != "sand-yak":
                    while enemy.health > 0:
                        self.attack(enemy)

            enemyIndex += 1
        loopCounts += 1

i hope you can help me…
[sorry if my english is bad, it’s not my native language]

– Kageyoshi


#2

Put the statement enemiesIndex = 0 in your loop: statement to reset the value to 0 each time you loop.

loop:
        enemies = self.findEnemies()
        enemiesIndex = 0
        while enemiesIndex <= len(enemies):

#3

Thanks for your answer
I tried your correction, but I got the same error again =/

If you want to try the code yourself, it is this level: Harrowland


#4

What if you run out of archers to attack?

Also, you are indenting enemyIndex while using enemiesIndex. You should change one or the other so that they match.


#5

There are multiple possible reasons for errors.

  1. The enemiesIndex = 0 and loopCounts = 0 should be inside the loop

  2. while enemiesIndex <= len(enemies) will raise a nullpointer error. What this means is, you start at index 0 which is the first spot in an array and move up to the last spot.

Lets say I have arr = [a,b,c]
My array points to a at index 0. but len(arr) is 3. so as you iterate:
Index0 = a, Index1 = b, Index2 = c… but then you are measuring by len, so it tries to find Index3 = null and an error will raise.
To fix this, do “while enemiesIndex < len(enemies)” or “while enemiesIndex <= (len(enemies) - 1)”

  1. While it’s nice you thought of the “while enemy.health > 0:” I believe self.attack(enemy) already attacks until the target is dead, thus making the while loop unneeded.

  2. Is a sand-yak an enemy? If not, you don’t need to check it in the list, since the list is populated only with enemies.

Now with these changes, particularly the first one, you will need to change the while loop, because the loopCount will always be set to 0 before entering the while loop.

My suggestion:

loop:
  enemies = self.findEnemies()
  Index = 0

  while Index < len(enemies):
    enemy = enemies[Index]
    if enemy.type == "archer": 
      self.attack(enemy)
    Index += 1

  Index = 0 
  while Index < len(enemies):
    enemy = enemies[Index]
    if enemy.type != "sand-yak":
      self.attack(enemy)
    Index += 1

This worked when I tested it, but be aware that bypassing the other units is dangerous and without enough health, you may die.

I provided this code since the thread is 20 days old.


#6

len(enemies) just refers to the length of the array, enemies. He didn’t enumerate it. In your example, the index would have been at 3 for Index2.

Actually, it doesn’t; rather, it does a certain amount of damage depending on your primary weapon.

Sand-yaks are enemies, but they don’t attack unless they are attacked. Their whole purpose for existence is to be skipped in while-loops.

Thanks, but please don’t post correct code. It takes the whole point out of the game. :frowning:

I’m actually not sure what level Kageyoshi is on. I’m well into Cloudrip Mountain and I haven’t seen a level with both enemy archers and sand-yaks. Perhaps he is confusing archers and throwers. EDIT: Actually, I have seen one but forgot it.


#7

Perhaps they are on Clash of Clones. That level has both.