I need help beating Dust in world 3 (Python)

You define enemy outside the loop. That means, to your character, there is only one enemy. Move it inside the loop. That should work.

1 Like

I did that, but now I wont go to the ambush point.

The while-loop quits as soon as the condition is false. Therefore, you don’t need that if-statement. Delete it and move your moveXY command to the 0th level of indentation, that is, no indentation.

Also, I wouldn’t cleave, as that will kill off a bunch of enemies and still count as one hit.

hits = 0
while hits < 10:
    enemy = self.findNearestEnemy()
    self.attack(enemy)
    hits + 1
    if self.isReady("throw") and self.distanceTo(enemy) < self.throwRange:
        self.throw(enemy)
        hits + 1
self.moveXY(79,33)

With this code it is attacking forever

Edit : I removed the loop

I could be wrong but I keep seeing a “loop:” with no way to leave it once you go in…

you do see a “loop” in my code.

That loop does not need to be there. The while-loop will repeat as long as the condition is true. Remove the loop: and your program should work.

I removed it and its still not working. I also edited my code. now its

hits = 0
while hits < 10:
    enemy = self.findNearestEnemy()
    self.attack(enemy)
    hits + 1
    if self.isReady("throw") and self.distanceTo(enemy) < self.throwRange:
        self.throw(enemy)
        hits + 1
self.moveXY(79,33)

What happens when you run your code?

Omarn just throws and attacks until he dies.

This is not valid it will not increase hits and so the while loop never stops):

hits + 1

What you mean is:

hits += 1 # or the long version: hits = hits + 1

You could also improve a bit the structure of your code. This is what it currently does:

  • attack
  • increase hits
  • if throw is ready: throw and increase hits

You could do something like:

  • if throw is ready: throw
  • otherwise (else): attack
  • increase hits
1 Like

Thanks! Now i beat this level.

i need help, my hero just goes to attack one munchkin and goes to the point right after.


what is wrong

2 Likes

Use hits == 10 instead of hits = 10–the == is the equality comparison and the = is the assignment operator. (Programming is great, right?)

2 Likes

yes it is great and awesome

i didn’t need to use that nick, i just needed to put a greater than sign before the equal sign on line 9

Actually you don’t “need” the entire “if - break” as it is totally redundant . . . that is the job of “while < 10”

(yes, it is pseudo/short-hand code)

loop:
    blah
    blah
    if >= 10:
        break

is the same as

while < 10:
    blah
    blah