[SOLVED] Can't solve Sarven Shepherd

See if this helps:

# Use while loops to pick out the ogre
while True: # This is your outer (parent) loop
    enemies = hero.findEnemies()
    enemyIndex = 0
    if enemyIndex < len(enemies): # Change this to a while loop; it is an inner loop, or child loop
        enemy = enemies[enemyIndex]
    # "!=" means "not equal to."
    if enemy.type != "sand-yak": # Need to indent these, as they are members of the 1st child loop
        # While the enemy's health is greater than 0, attack it!
        while enemy.health > 0: # This is another inner loop, or a step-child, as it is a member of the 1st child loop
            hero.attack(enemy)
        pass
    enemyIndex += 1
# Between waves, move back to the center.
hero.moveXY(40, 32)

I made the alterations suggested above and your code works!

Didn’t work, @dedreous.
Any other alterations?

Let’s see your new code…

Same as your’s but without the comments:

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

Hero just stands still

You didn’t change the if to a while…line 4, as counted from the above code. You also didn’t indent line 5-9, to make that block a child of the new while loop. Finally, you didn’t indent line 12…it is the final statement of the outer loop.

If I change the if to a while in line 4, an infinite loop occurs.
Can you please show me any code that rectifies this?
Also, here’s my code:

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

I PM’d you your original code, with the necessary modifications.

Solved it:

# Use while loops to pick out the ogre
while True: # This is your outer (parent) loop
    enemies = hero.findEnemies()
    enemyIndex = 0
    while enemyIndex < len(enemies): # Change this to a while loop; it is an inner loop, or child loop
        enemy = enemies[enemyIndex]
        # "!=" means "not equal to."
        if enemy.type != "sand-yak": # Need to indent these, as they are members of the 1st child loop
            # While the enemy's health is greater than 0, attack it!
            while enemy.health > 0: # This is another inner loop, or a step-child, as it is a member of the 1st child loop
                hero.attack(enemy)
            pass
        enemyIndex += 1
    # Between waves, move back to the center.
    hero.moveXY(40, 32)

Thanks, @dedreous

Great…now please delete the code above…we don’t post solutions.

Yes and @CodingGeek14, I’m afraid I’m also going to ask you to delete the solution in this topic:
https://discourse.codecombat.com/t/python-help-anyone/20604/10
I know it’s a bit annoying, but we can’t have solutions readily available for peoples use or it could ruin the game for some people.
Thanks
Danny

One problem:

ZAX155

2h

here is my code so far

# Use your new "cleave" skill as often as you can.

hero.moveXY(23, 23)
while True:
    enemy = hero.findNearestEnemy()
    if hero.isReady("cleave"):
        # Cleave the enemy!
        hero.cleave(enemy)
        pass
    else:
        # Else (if cleave isn't ready), do your normal attack.
        hero.attack(enemy)
        pass

and:

ZAX155

1h
it worked though so what now

Also, I edited the comment you showed me so that it doesn’t show the solution.
Plus, his code before my comment worked anyway and he didn’t want my solution.

could you explain what you want from me in more detail

Ok, thanks. Sorry if I’ve caused any misunderstanding.
Danny

Don’t worry, I was just explaining to @Deadpool198 about our conversation in: Python help with anyone.

It’s OK, @Deadpool198

hi I need help with my code:

return  #Commented out to stop infinite loop.
# Use while loops to pick out the ogre
while True:
    enemies = hero.findEnemies()
    enemyIndex = 0
     
    # Wrap this logic in a while loop to attack all enemies.
    # Find the array's length with:  len(enemies)
    while enemyIndex < len(enemies):
        enemy = enemies[enemyIndex]
        # "!=" means "not equal to."
    enemy = hero.findNearestEnemy()
    if enemy.type != "sand-yak":
        # While the enemy's health is greater than 0, attack it!
        while enemy.health > 0:
            hero.attack(enemy)
        pass
    # Between waves, move back to the center.
    hero.moveXY(40, 32)


the hero does nothing.

Think you need to increase enemyIndex each time you go through the loop…

how @jka2706? (20 characters)

enemyIndex = enemyIndex + 1 like this?

Think so (I’m mostly JavaScript, but that looks right).

1 Like