Sarven Shepard -- Python

There is no error for this one. My character just stands still, like nothing is happening. Confusion??

# Use while loops to pick out the ogre
while True:
    enemies = hero.findEnemies()
enemyIndex = 0
for enemy in enemies:
    hero.attack(enemy)

# Wrap this logic in a while loop to attack all enemies.
# Find the array's length with:  len(enemies)

enemy = enemies[enemyIndex]
# "!=" means "not equal to."
if enemy.type != "sand-yak":
    # While the enemy's health is greater than 0, attack it!
    hero.attack(enemy)
    pass

# Between waves, move back to the center.
hero.moveXY(42, 32)

Can you send a link to the level?
Lydia


boom

Your indentation is wrong. All the code you want to run more than once should be inside the while True loop. That’s all of it. Also, you can either use a for loop, or a while loop (which will need the enemyIndex. You can’t switch between them. You’re also attacking twice. It looks like two different strategies. Just chose one, loop through the enemies, attack the non-yaks and move back to the centre after the for loop has finished running.
Danny

in sarven, you haven’t learned for loops yet, there mountain.

yeah you get it by wining clash of clones

Yes you do, while loops is one of important concept in sarven desert.

he meant for loops, not while loops(you get it in mountain)

proof? boom.

no for loops!!!

Don’t worry about that try using while-loops for now until you beat clash of clones

Ok. (20 charrrrrrrr)

So basically, I put everything into the while true loop and my character attacks a sand yak. Why?

I know, I thought you mean just loops. Not specifically for loops.

oh i need to post code im an idiot

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


don’t use the for-loop you don’t have it and you don’t understand it yet. I did it without the for-loop

because you haven’t gotten it

Hi AlwaysConfused,

Your hero is attacking the sand-yaks because your line 7 tells the hero to attack each enemy. Delete this line, and just use the attack command in line 14.

I think you can choose to use a while loop or a for loop (when I tried it it worked with both). However you need to stick to one or the other, as you seem to have bits of code that are trying to do both.

The other thing you need to do is sort out your indentation. This would be easier to see if you go back to the while loop, as the comments are all written in the right place; but you could work it out for the for loop if you wanted. Which bits of code are running inside other conditions?

Jenny

1 Like

1, thanks for the first thing,
2, i havent gotten for loops yet
3, i did indent it correctly
4, all of it? haha
5, now the problem is that he gets stuck in his walking animation… but doesnt do the walking in my “move back to the center code”…
6 If i delete my “move back to the center code” he does literally nothing- NOTHING

code and screenshot

# Use while loops to pick out the ogre
while True:
    enemies = hero.findEnemies()
    enemyIndex = 0
    # Find the array's length with:  len(enemies)
    enemy = enemies[enemyIndex]
    # "!=" means "not equal to."
    if enemy and enemy.type != "sand-yak":
        # While the enemy's health is greater than 0, attack it!
        while enemy.health > 0:
            hero.attack(enemy)
    # Between waves, move back to the center.