[SOLVED] Lurkers - Python

Hey! So my code is this, my hero doesn’t even MOVE.

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

enemies = hero.findEnemies()
enemyIndex = 0

# Wrap this section in a while loop to iterate all enemies.
# While the enemyIndex is less than the length of enemies
while enemyIndex < len(enemies):
    enemy = enemies[enemyIndex]
    if enemy.type == "shaman":
        while enemy.health > 0:
            hero.attack(enemy)
    elif enemy and enemy.type == "yak":
        hero.say("Hi.")
# Remember to increment enemyIndex
enemyIndex += 1

1 Like

can I have a link to the level

1 Like

https://codecombat.com/play/level/lurkers?

1 Like

delete that part about the yaks

2 Likes

Put the enemyIndex += 1 into the while loop

2 Likes

Thanks, @Code_Master

1 Like

Did it work? Please click the checkbox beside the person who you think helped the most if it worked.

1 Like

you can actually put it on two people

2 Likes

err… no it didnt work. my hero is still just standing there

1 Like

I have had the same problem before, try moving to the shamans and attacking them.

1 Like

Try also putting the comment in the while loop too.

try doing this
if enemy.type == ‘shaman’

1 Like

You can just delete this.
Lydia

1 Like

If you are still just standing there, I’m guessing you didn’t properly follow @Code_Master tip. Does your run bar always show “Running” and do you get an error for an infinite loop?

When I reset my level, I noticed the indentation was all shifted at the beginning which can be confusing. Looking at your original code, to include it in the while loop you need to have that line of code indented one level, otherwise you will have an infinite loop since you are always comparing 0 to the length of the enemy array.

Yes, precisely, it said running and warned me of an infinite loop when I reset the browser.

Here is my code again, it reset because a notification popped up, claiming my code had an infinite loop. I clicked the wrong button and I was back at the start. I took the advice you gave me and here’s what I have, however, THE HERO STILL DOESNT MOVE.

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

enemies = hero.findEnemies()
enemyIndex = 0

# Wrap this section in a while loop to iterate all enemies.
# While the enemyIndex is less than the length of enemies
enemy = hero.findNearestEnemy()
if enemy.type == 'shaman':
    while enemy.health > 0:
        hero.moveXY(enemy.pos.x, enemy.pos.y)
        hero.attack(enemy)
        enemyIndex += 1

I see you removed the while enemyIndex < len(enemies):

You need this part to go through all the enemies, the issue was your enemyIndex += 1 was in the wrong indentation.

Ah! I see now. I passed the level.

Congrats! Check the tick mark next to the comment that helped you the most!
Lydia

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.