Infinite loop while using while loops

I am trying to teach myself while loops in the cavern survival level.

I was running this code and it hung up with an infinite loop error. It did let me comment out the code. Am I returning out of while loop incorrectly? I figured that placing the instructions for what to do when n>4 on the same indent level as the while loop would work… but I’m working from incomplete knowledge here. I was hoping it would repeat “Done with while loop” while casting shockwave at enemies when the while loop was complete. Advice?

return;  // Commented out to stop infinite loop.
return;  // Commented out to stop infinite loop.
self.cast("haste", self)

    while n<5:
    # Find and attack the enemy inside a loop.
    # Use flags and your special moves to win!
        if self.canCast("haste", self):
            self.cast("haste", self)
        elif enemy:
            if self.canCast("root", enemy):
                self.cast("root", enemy)
            elif self.canCast("slow", enemy):
                self.cast("slow", enemy)
        if n==0:
            if enemy:
                self.cast("shockwave", enemy)
        if n==1:
            self.moveXY(102, 20)
        if n==2:
            self.moveXY(99, 116)
        if n==3:
        if n==4:
    self.say("Done with while loop")
    if self.canCast("haste", self):
        self.cast("haste", self)
    if self.canCast("shockwave", enemy):
        self.cast("shockwave", enemy)

Ah man BunchofCrooks!

That solution is a good one, maybe it’s a syntax problem rather than a logic problem?

Yeah, I was thinking it was syntax… did you get the same error?

I’m using JS, Let me check.

Yeah, not getting it either. Wondering if the coding for break; isn’t in yet and whether you could use that to shoot over to your “shockwave” and “loop is done” string

Yeah, there are likely quite a few ways to do it without the While loop. I’m just trying to learn how to use a while loop.

If there is no enemy (or none in range), you will never increment N and never leave the loop.

1 Like

Thanks! Nick helped me see the problem as well. I have it up and working now. The important thing is that I know how to use while loops now! achievement unlocked