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

Help with Sarven Shepherd [SOLVED]

#1

Hi,
I am stuck, the palyer attacks the first ogre but then he says: …but it’s dead.

Can you help me?

# Benutze while-Schleifen, um den Oger rauszusuchen

while True:
    enemies = hero.findEnemies()
    enemyIndex = 0
    
    # Verbaue diese Logik in einer While-Schleife, um alle Gegner anzugreifen.
    # ermittele die Länge des Arrays mit: len(enemies)
    while True:
        enemy = enemies[enemyIndex]
        # "!=" bedeutet "ist nicht gleich."
        if enemy:
            enemyIndex += 1
            if enemy.type != "sand-yak":
                # Greife den Gegner an, solange seine Gesundheit größer als 0 ist!
                if enemy.health >= 0:
                    hero.attack(enemy)
            pass
    
    # Gehe zwischen den Wellen zurück zur Mitte.
    hero.moveXY(40, 32)
    
#2

Please give more information about your code. A screenshot and any errors given when you run your code. Also a little bit more information about what happens when you run your code. Thanks!

-@Luke10

#3

He defeats the 1st Enemy but then he doesn’t attack the Second Enemy, instead he is saying: “…but it’s dead”

1 Like
#4

I think I see two different details that could use some modification.

The second while True loop causes your hero to keep attacking the second enemy “… but it’s dead”. Instead of True use the comparison enemyIndex and len(enemies) to break from the second while loop. Also, I see notes to use a while health is greater. Switch the if to while and your hero will attack that one enemy until it is dead.

while enemy.health >= 0:

While this didn’t cause issues on this one, keep this in mind for later
The enemyIndex starts at 0 for the first enemy (correct), but before you call the attack command, you increase the index and skip the first enemy.

        if enemy:
            enemyIndex += 1 # skips over first enemy [0] before attacking
            if enemy.type != "sand-yak":
                # Greife den Gegner an, solange seine Gesundheit größer als 0 ist!
                if enemy.health >= 0:
                    hero.attack(enemy) # attacks second enemy [1]
#5

Thank you so much, it is now functioning :slight_smile: