While is a problem

Maybe I am just impatient. I try to survive in the cave my first time. So in the moment I have this:
i = 0
hero.shield()
while i < 4:
----enemy = hero.findNearestEnemy()
----hero.attack(enemy)
----i +1
hero.moveXY(51,41)

my problem is:
after I kill the first wave my stupid hero (or is it me who is stupid? ;)) have no wish to go to 51,41 and I have no clue why. Anybody have an idea?

i = 0
hero.shield()
while i < 4:
    enemy = hero.findNearestEnemy()
    hero.attack(enemy)
    i +1 # you do "+=" instead, add AND ASSIGN
hero.moveXY(51,41) # This line never happens, change the code above
#then it will happen

instead, do a for-loop, the code below is all u need

hero.shield()
for i in range(4):
    hero.attack(hero.findNearestEnemy())
hero.moveXY(51,41)

here’s how it works

for i in range(4): #===> repeat 4 times
1 Like

what do you mean with “ASSIGN”`?
if I would use a assign then the interpreter would read the move line?

also works for lengths and indexs

for enemy in enemies:
    #do stuff

you need “+=” not “+”

have += the same meaning like == just in a other way?

u cant just do i + 1. i+1 = ??. u need to assign it

kind of, but adds 1 every time

just use the for loop

ok, before i started to learn more about while I was learning if and else and there u can say x +1. that was the idea behind. But for this problem I will do like u said, thanks :slight_smile:

number += 1
means the same as
number = number + 1
(means the same as
increase number by 1)

1 Like
while True:
    enemies = hero.findEnemies()
    Index = 0
    while Index < len(enemies):
        enemy = enemies[Index]
        while enemy.health > 0:
            hero.attack(enemy)
        Index += 1 # Move on to the next enemy to attack

or just use

while True:
    enemies = hero.findEnemies()
    for enemy in enemies:
        while enemy.health > 0:
            hero.attack(enemy)
1 Like

for loop is basically the same thing but WAY easier

1 Like

@EpicCoder999 thanks for all this help. I appreciated very much to read all of this :smiley: Thanks very much. Somehow I got just unlucky that I could not answer anymore.

ur welcome and its ok