Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

What's the order of events for nested While loops in Python?

I’m teaching myself Python and I’m trying to make a basic game using RPG mechanics I’m familiar with. My core mechanic looks like this:

while not GameOver():                    #checking for one side or the other to be all KO'd
    turnbegin()                          #resetting # of moves per player, etc
    while not TurnDone():                #checking to see if everyone's out of moves
        for ch in activechars:           #going through the players who still have moves
            if ch not in defeatedchars:  #ignoring the KO'd players
                attack(ch,target(ch))    #EVERYONE PUNCH EVERYONE (keeping it simple)
            else:
                pass

My problem is that this loop is still trying to run the target(ch) function after it should’ve hit GameOver(). The counters are down (everyone’s KO’d) and the GameOver function seems to be working correctly; I checked. But GameOver returns True and then…it just rolls on to attack() and kicks back an error that it doesn’t have anyone to target instead of just stopping because it’s over. I tried creating a gameover=GameOver() variable and saying "while not gameover" instead, but then it just got stuck in turnbegin() after it said turn 2 began.

Thank you for reading this! I’m very new to this and very thankful for help.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

"while not GameOver()" is only evaluated when it finishes running and needs to enter another loop.

Since TurnDone() is still true, it doesn’t exit the loop and GameOver() is not re-evaluated.

When GameOver() gets updated to True, then TurnDone() should become True as well to prevent it from entering another loop.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading