Help with my lurkers code: : -\


#1

So I have been stuck on the lurkers level for to many hours to count. I have tried and lost so much code tinkering with it. So I would love to catch some advice and figure out where my code goes wrong.


enemies = self.findEnemies()
enemyIndex = 0

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

Can I get some advice friends?


Stage: DESERT Level: LURKERS
#2

Try incrementing enemyIndex even if it is a shaman, or you’ll always be stuck on the first shaman. (So just don’t use else.)


#3

I’ve had trouble here as well.

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

on the line while enemyIndex <= 3, the program says “unexpected token”. I would love to know what I am doing wrong!

Thanks so much!


#4

So I gave it another fair shot tonight. Two of us actually. Anyways were both still lost.

Code in current format

enemies = self.findEnemies()
enemyIndex = 0

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

#5

You’re incrementing enemyIndex at the wrong place.
Try the following:

enemies = self.findEnemies()
enemyIndex = 0

while enemyIndex < len(enemies):
    enemy = enemies[enemyIndex]
    enemyIndex += 1   
    #Note: You have to increase the index in every case, even if it's no shaman.
    if enemy.type == 'shaman':
        #gently 'awaken' him

#6

It says “unexpected token” because you missed the : at the end of the line.
A while-loop always looks like this

while condition:
    #do something

where condition can be any expression that is truthy/falthy. Comparisons for example, like enemyIndex < 3


#7

I figured it out

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

enemies = self.findEnemies()
enemyIndex = 0

# Wrap this section in a while loop to iterate over all enemies.
while enemyIndex < 6:
    enemy = enemies[enemyIndex]
    if enemy.type == 'shaman':
        pos = enemy.pos
        x = pos.x
        y = pos.y
        self.moveXY(x, y)
        self.say("Wake up")
        enemyIndex += 1
        while enemy.health > 0:
                self.attack(enemy)
    else:
        enemyIndex += 1

#9

I need help with this too I can’t seem to get the right answer for the end. here is what I have
// findEnemies returns a list of all your enemies.
// Only attack shamans. Don’t attack yaks!

var enemies = this.findEnemies();
var enemyIndex = 0;

// 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) {
var enemy = enemies[enemyIndex];
if (enemy.type == ‘shaman’) {
while (enemy.health > 0) {
this.attack(enemy);
}
}
}


#10

And an

enemyIndex++;

just before the end of the first while.

So before the last bracket {

The standard while over a list:

index=0;
while (index < list.length){
     elem = list[index]
     \\do something with the elem
     index++
}