Play Home Contribute Github Join Us! Discourse Staff Members Github Contribution Guides Team

Stuck on "Sarven Shephard" [SOLVED]

#1

Here is my code. I don’t know what’s wrong.
while True:
enemies = hero.findEnemies()
enemyIndex = 0
while enemyIndex < len(enemies):
enemy = enemies[enemyIndex]
if enemy.type != “sand-yak”:
while enemy.health > 0:
hero.attack(enemy)
enemyIndex += 1
pass
hero.moveXY(40, 32)

0 Likes

#2

Please learn to post your code correctly. The way it is now, we can’t see the structure. Help us help you. It’s very easy to do and just takes a tiny bit of effort. Please read this topic and format your code again correctly

0 Likes

#3

Ok here is my code and thanks for the tip!

while True:
    enemies = hero.findEnemies()
    enemyIndex = 0
    while enemyIndex < len(enemies):
        enemy = enemies[enemyIndex]
        if enemy.type != "sand-yak":
            while enemy.health > 0:
                hero.attack(enemy)
    enemyIndex += 1
    pass
    hero.moveXY(40, 32)
0 Likes

#4

Right now you are circling through the same enemyIndex. The enemyIndex += 1 needs to fit in the while enemyIndex < len(enemies): loop.

enemyIndex += 1 # tab once more to include it in the loop
pass # tab again
0 Likes

#5

A little bit different logic to replace while enemy.health>0:. Not exact solution ( it’s java script and using i as enemyIndex). Put the code after while enemyIndex < len(enemies):

   // usual code
   var enemies = hero.findByType('scout');
   // usual code
   while ( // usual code.....
       var enemy = enemies[i];
       if (enemy.health > 0){
            hero.attack(enemy); 
            i--; 
        console.log('enemy:' + enemy.id + ' health:' + enemy.health );
       }       
       else  i++;
    }   
}

used the weakest sword -> screenshot from the console:
Yorga
Is that logic fail proof?
Edited when found an error

0 Likes

#6

Your console.log is after the last attack which will show the health below 0. Move the console.log before the attack enemy, and you will see the health does not go below 0.

health

0 Likes

#7

Yes , it’s after the last attack and I need this ‘coup de grâce’ to finish my task :slight_smile:
But I didn’t test this method in many levels and I search to find if it will fail sometimes?

0 Likes

#8

I don’t understand what you mean so could you please post an example of the correct code? BTW thanks for the help.

0 Likes

#9

Nevermind I figured it out thanks for the help! Here is my code for anyone else who is struggling:

Mod edit: Please do not post final solutions.

0 Likes

closed #10
0 Likes