Help with code "lurkers"


#1

I did wrote code, and pass through level

enemies = self.findEnemies()
enemyIndex = 0

while enemyIndex < 3:
    for enemy in enemies:
    if enemy.type == 'shaman':
        enemyIndex = enemyIndex + 1
        while enemy.health > 0:
        self.attack(enemy)

but it’s say about infinite loop.
explain my mistake please


#2

Well for starters you arent indenting properly.

You need to indent everything forward after the for and after the while.

Also i wouldn’t limit it to 3… you don’t know where in the enemies array those 3 shaman are. just do the loop while enemyIndex is less than or equals to enemies.length


#3

you never update “enemies” inside the loop. If you have less than 3 shamans in the enemy list, enemyIndex will never reach 3, and the loop will never exit.

Make sure “enemies” object is updated within the loop, or you need to handle a case where there are less than 3 shamans.


#4

Thanx guys.
I see the problem
I wrote while < 3 but I can kill only 3 shamans.
3 aren’t < 3 so endless loop.

I found werd thing

in this way of code i do kill three shamans

for enemy in enemies:
        if enemy.type == 'shaman':
        killed = killed + 1
            while enemy.health > 0: 
            self.attack(enemy)

in this way I will go to fight yak

for enemy in enemies:
        if enemy.type == 'shaman':
        killed = killed + 1
        while enemy.health > 0:
        self.attack(enemy)

difference is only with four spaces before second while

actually CodeCombat suggest me to line-up, but then code not works for me =)


#5

Your indenting is a mess. You need to revisit the levels that teach you about python indenting. Or go google it.

from what i can see in the screenshot. the killed line needs to be indented forward. but i cant see everything. try posting your whole code in the forum.

put three of these characters on a line by itself both before and after your code… `


#6

levels that teach you about python indenting not teach much. probably need a serious book


#7
enemies = self.findEnemies()
killed = 1

while killed < 3:
    for enemy in enemies:
        if enemy.type == 'shaman':
        killed = killed + 1
            while enemy.health > 0:
            self.attack(enemy)
            
self.say("done")

#8

it’s pretty simple.

think of languages where you would have {}

instead of curly braces you simply indent forward.

so

if (something) {
 // do this
}

becomes

if something:
  do this

#9

no. use the tilde mark next to the 1 key


#10

I’m not going to test your code. but just indenting alone. this is correct.

enemies = self.findEnemies()
killed = 1

while killed < 3:
  for enemy in enemies:
    if enemy.type == 'shaman':
      killed = killed + 1
      while enemy.health > 0:
        self.attack(enemy)
    
self.say("done")

#11

whatever. thank you. :penguin:


#12

Hey just wondering if someone can help me out with this code i beat it using Python but Java is giving me probs ( i always get the error Hard execution limit 30000)
Heres my Code:

// Only attack shamans. Don't attack yaks! var enemies = this.findEnemies(); var enemyIndex = 0;
while (enemyIndex < 3) {
    var enemy = enemies[enemyIndex];
    if (enemy.type == 'shaman') {
        enemyIndex = enemyIndex + 1;
        while (enemy.health > 0) {
            this.attack(enemy);
        }
    }
}

#13

What happens if enemy.type is not 'shaman'. Think about it and you should see your problem.


#14

ahhh yes thank you I see my mistake