0
done=False

while not done:
    for level in range(1,13):
       Code...
        while running:
            Code.....

            #10 - Win/Lose check

            if healthvalue<=0:
                done=True
                running=0
                exitcode=0
                print "aaa"

I have got this game on python which when the health is lower than 0, it should quit the loop. However, even though I state it so that done=True in the if statement, the loop still does not quit although running does become 0. I also checked by printing "aaa", and it does print, but yet done does not equal True in order to quit the loop.

Please help! Does it have something to do with the for loop?

Masa Tono
  • 49
  • 6
  • 1
    Please read [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). – GingerPlusPlus Oct 29 '15 at 17:13
  • 1
    You need to show the rest of your code. If you're resetting `running` or `done` anywhere it'll cause problems. And yes, unless you manually break out of the `for` loop, that will continue. – tzaman Oct 29 '15 at 17:14
  • What's wrong with the question. My question does follow all of those criteria. – Masa Tono Oct 29 '15 at 17:15
  • @MasaTono: We can't copy your code and run it with no changes. – GingerPlusPlus Oct 29 '15 at 17:15
  • @tzaman I thought if I show the whole code, it will be too long. – Masa Tono Oct 29 '15 at 17:16
  • You need show the relevant sections -- everywhere you manipulate your loop control variables, at the very least. – tzaman Oct 29 '15 at 17:18
  • Debug it: `import pdb; pdb.set_trace()`. The error isn't in the part you are showing us (unless you aren't properly handling the `for level...` loop. – KurzedMetal Oct 29 '15 at 17:19
  • @tzaman that was all of the manipulation of the loops. The person below answered my question as I did not realise that you had to break the for loop as I thought that if I broke the while loop, the for loop will automatically break. – Masa Tono Oct 29 '15 at 17:26
  • @GingerPlusPlus Well I didn't realise that I had to break out of the for loop – Masa Tono Oct 29 '15 at 17:28

1 Answers1

1

If you have layers of loops, you need to break out of each one.

done=False

while not done:
  for level in range(1,13):
       Code...
       # add "not done" as a condition to get out of this loop:
       while running and not done:
            Code.....

            #10 - Win/Lose check

            if healthvalue<=0:
                done=True
       # break out of the for loop
       if done == True:
         break
ergonaut
  • 6,929
  • 1
  • 17
  • 47
  • I didn't realise that you had to break the for loop. I though if I break the while loop, the for loop will automatically break. – Masa Tono Oct 29 '15 at 17:24
  • If that was the case, then there would be no way to continue processing in one of the loops. – ergonaut Oct 29 '15 at 17:27