I need help beating Dust in world 3 (Python)


#1

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!


#2

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.


#3

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

Nevermind I got it thank you for the help.


#5

moste easy way is

hits += 1

means:

hits = hits + 1

#6

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.


#7

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


#8

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

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


#9

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


#10

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.


#11

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.


#12

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


#13

Still attacks one enemy and stands there…


#14

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


#15

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 ?


#16

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


#17

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


#18

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


#19

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.


#20
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.


#21

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