I need help beating Dust in world 3 (Python)

Always take an action inside a while loop, or it’ll go infinite!

Use while to loop until you have enough hits to kill 10 munchkins.

hits = 0
while hits < 10:
enemy = self.findNearestEnemy()
if enemy:
self.attack(enemy)
while hits >= 10:
self.moveXY(79, 33)

This is my code. I can’t figure out why it won’t work. My character just keeps killing ogres and doesn’t ever retreat. Please help! Thanks!

In the future, please surround your code in triple back ticks, like this:

#```
#Put your code here
#```

You never increase the variable hits. Therefore, it will always equal zero.

I’m unsure of how to increase variable hits. Can you tell me how?

Nevermind I got it thank you for the help.

moste easy way is

hits += 1

means:

hits = hits + 1

I am really confused on this level. I’ve either got a slow or infinite loop in my code. PLEASE HELP! I even did what the while-loop option says what to do to a while loop in the bottom right corner.

Well, what is your code? We can’t help you without knowing exactly what is causing the error.

enemy = self.findNearestEnemy()
while hits <10:
hits = 0
self.attack(enemy)
hits +=1

loop:
if hits >= 10:
self.moveXY(79,33)

I don’t know how you show the game code from the site. lol :smile:

Well, you could always read the FAQ, as all new users should.

You don’t need the second loop, the while loop automatically breaks once the condition you set is satisfied.

So, I got rid of the second loop, but my hero now just attacks one enemy then stands there without attacking other enemies around him.

You are using the variable hits before it is actually defined. Try putting hits = 0 before the while-loop.

Still attacks one enemy and stands there…

What is your code now? Is there any error that registered?

No error on first run then slow or infinite loop afterwards.

hits = 0
enemy = self.findNearestEnemy()
while hits < 10:
    loop:
        self.attack(enemy)
        hits += 1

    if hits >= 10:
            self.moveXY(79,33)

could I put hits += 1 as hits ++ 1 ?

If you are incrementing by 1 just use hits++;

Figured it out! Finally! I finished the level! Thank you @ChronistGilver for your help.

I am having trouble with this level too. can someone help me with it?

Well, Autri_Basu, just saying “I need help!” and waiting for us to solve all your problems isn’t good policy, because we usually don’t know what, exactly you need help with. Post your code, along with a description of the error and a screenshot or two if necessary.

hits = 0
enemy = self.findNearestEnemy()
while hits < 10:
    loop:
        if self.isReady("cleave"):
            self.cleave(enemy)
        else:
            self.attack(enemy)
        hits += 1

    if hits >= 10:
        self.moveXY(79,33)

The issue is that I cleave once and then just stand still.

This is my other coding

hits = 0
enemy = self.findNearestEnemy()
while hits < 10:
    loop:
        self.attack(enemy)
        hits += 1
    if hits >= 10:
        self.moveXY(79,33)

(this is on Omarn)
he just attacks twice and then stands still