Teach me to code! lvl sarven-shepherd

Guys I don’t get how to use arrays and a while cycle.
My guy that stand one place and doing nothing.

# Use while loops to pick out the ogre
loop:
    enemies = self.findEnemies()
    enemyIndex = 0
    # Wrap this logic in a while loop to attack all enemies.
   enemy = enemies[enemyIndex]
    # "!=" means "not equal to."
    if enemy.type != "sand-yak":
        # While the enemy's health is greater than 0, attack it!
 pass
 # Between waves, move back to the center.

so I did by my own

loop:
    enemies = self.findEnemies()
    
    for enemy in enemies:
        if enemy.type != 'sand-yak':
            self.attack(enemy)
            self.powerUp()
            self.bash(enemy)
            

and i kill all stinky ogres in 35.3s (2nd place for today at this level, guys)

so I did level but don’t get anything =(

2 Likes

i did firtly this way

loop:
enemies = self.findEnemies()
enemyIndex = 0

while index < len.enemies kinda add this line here

enemy = enemies[enemyIndex]
if enemy.type != “sand-yak”:
while health > 0
atack and here

pass
else move center

2 Likes

yeah

while enemyIndex < len(enemies):
    enemy = enemies[enemyIndex]
    #and don't forget the GOTCHA!!!
    enemyIndex += 1
    #rest of enemy stuff
2 Likes

thanx, but it’s some how not works for me =((((

2 Likes

I am also having trouble with this level and I can’t seem to figure out which variable to use with the while loop. Can someone help me?
This is my code

# Use while loops to pick out the ogre

loop:
    enemies = self.findEnemies()
    enemyIndex = 0

    # Wrap this logic in a while loop to attack all enemies.
    while enemyIndex:
    enemy = enemies[enemyIndex]
    # "!=" means "not equal to."
    if enemy.type != "sand-yak":
        # While the enemy's health is greater than 0, attack it!
2 Likes

In this case you would be using the index, since it is the least complicated and the other variable would [essentially] be index based anyway so…

index = somenumber
while index compares to something
    do stuff with index
    inc/dec-rement index
    do other stuff

If you only use “index” near the top of the while loop, I’d suggest inc/dec-rementing it there, so you know it is done, because if you forget —> inf-loop!!

2 Likes

I am very sorry, but I have no idea what any of that means. Also, when I tried to do

        if self.isReady("throw") and self.distanceTo(enemies) < self.throwRange:

It says on the error bar that you need to find the distance to a target unit. what does that mean

2 Likes

The problem may be that enemies is a list of enemies, not a single enemy. Try to define a single enemy first:

enemy = self.findNearest(enemies)

Then fix your code to say:

... self.distanceTo(enemy) ...

You should probably also check if there is any enemy:

if enemy:
    # put the rest here
2 Likes

ok thanks for the help.

2 Likes

this is my current code

# Use while loops to pick out the ogre

loop:
    enemies = self.findEnemies()
    enemy = self.findNearest(self.findEnemies())
    enemyIndex = 0

    # Wrap this logic in a while loop to attack all enemies.
    while enemyIndex < 10:
    enemy = enemies[enemyIndex]
    # "!=" means "not equal to."
    if enemy.type != "sand-yak":
        while enemy.health > 0:
            self.attack(enemy)       
        pass

    # Between waves, move back to the center.

I really have no idea what to do

2 Likes

RIght now, the loop: is blocking your code. All that’s happening is the first three commands happening over and over again. Also, in Python, whitespace actually matters, so indentations are important. Fix those, then come back if you still have an error.

2 Likes

I have this now and the same is still happening

# Use while loops to pick out the ogre
enemyIndex = 0
while enemyIndex < 10:
# "!=" means "not equal to."
if enemy:
    if enemy.type != "sand-yak":
        while enemy.health > 0:
            self.attack(enemy)
            enemyIndex += 1
else:
    self.moveXY(40, 31)
2 Likes

Indentations are still not right. The if/else is not inside the while-loop.

2 Likes

New code:

# Use while loops to pick out the ogre
enemyIndex = 0
while enemyIndex < 10:
    enemy = self.findNearest(self.findEnemies())
# "!=" means "not equal to."
    if enemy:
        if enemy.type != "sand-yak":
            while enemy.health > 0:
                self.attack(enemy)
                enemyIndex += 1
    else:
        self.moveXY(40, 31)

now i got slow/infinite loop.

2 Likes

you only increment the index when the enemy is not a sand-yak . . . . so if it is a sand-yak it loops infinitely checking and rechecking and rerechecking the same sand-yak.

2 Likes

I see several things to improve.

First you need to find all enemies:

enemies = self.findEnemies()

Then you want to loop through all the enemies, no matter how many there are (not 10):

# start counting the enemies from zero:
enemyIndex = 0
# while we check all enemies (0, 1, 2, 3...) do this:
while enemyIndex < len(enemies):
# len(enemies) tells how many elements (=enemies) are in the "enemies" list
    enemy = enemies[enemyIndex]
    # enemies[0] is the first element (=enemy) in the list
    # enemies[1] is the second, etc...

Then you check if the enemy is not a sand yak, attack it until it’s dead and increase the index (= the “counter”): this mostly done in your code.

Finally when you went through all the enemies (and either killed them or skipped them), go back to the center. Note: this should be outside your while block!

Oh, and wrap the whole code in a loop, because there are multiple waves of enemies…

2 Likes

New Code (I am very new to coding)

loop:
    enemies = self.findEnemies()
    enemyIndex = 0
    while enemyIndex < len(enemies):
        enemy = enemies[enemyIndex]
        if enemy:
            if enemy.type != "sand-yak":
                self.attack(enemy)
                if self.canCast("drain-life", enemy):
                    self.cast("drain-life", enemy)
self.moveXY(40, 33)

Says the hard execution limit of 3,000,000 is exceeded

2 Likes

You never increase enemyIndex anywhere. I can’t be greater than len(enemies) ever.

2 Likes

I incremented enemyIndex and still is just standing still with the limit of 3mil

1 Like

The if enemy check is not necessary here: if there are no enemies, the while loop stops immediately, because len(enemies) = 0 :

while 0 < 0:
    # blabla

You forgot to increase the index. It’s probably best to do right after you used it:

enemy = enemies[enemyIndex]
enemyIndex += 1
# blabla

The moveXY() should be part of your loop. Currently you’ll never do it, because the loop never stops.

loop:
    # blabla

    while ...
        # blabla

    moveXY(...)

You should also restructure the attacking part a bit, because currently you attack the enemy and then cast drain-life if it’s ready. However, if the enemy was killed with your attack, you’ll get an error message when you try to cast… Also, after you hit the enemy, you switch to the next one, and then the next one, and so on, without killing them.

Probably it’s better to keep it simple, as in your original code:

while enemy.health > 0:
    self.attack(enemy)
4 Likes