Python while loop not stopping?
Question: I'm trying to make a dice 21 game (look up if you need to, it's too long to
type out here) on Python. It's not finished yet, but for now I'm going through
and fixing any mistakes I made. I'm having some issues with a while loop that
won't turn off. After the player chooses to stick in the diceroll function, it
should set playeraddrolls to False and exit out of the while loop, into the
computeroll function. However, it just cycles back. Immediate help is needed
because this is a school project in for Monday, after I still have to finish
the code. It would also help a lot if you could point out any additional
errors I will come across later, and how to fix them.

    
    
    import random
    stick=0
    winner=[""]
    
    def diceroll(addedrolls,stick,playagain,playagain1or2,playeraddedrolls,computeraddedrolls,playeraddrolls):
        while playeraddedrolls<21 or playeraddrolls is True:
            stick=0
            die1=random.randint(1,6)
            die2=random.randint(1,6)
    
            print("You rolled ",die1,"and ",die2,".")
            playeraddedrolls=die1+die2
            if playeraddedrolls>21:
                print("You rolled over 21. Computer wins by default!")
                computeraddedrolls(playeraddedrolls,playagain,playagain1or2,computeraddedrolls)
            else:
                while stick>2 or stick<1:
                    stick=int(input("Press 1 to stick or 2 to roll again. "))
                    if stick==1:
                        print("You chose to stick at", playeraddedrolls,". The computer will now roll.")
                        playeraddrolls=False
                        computeroll(playeraddedrolls,playagain,playagain1or2,computeraddedrolls)
                    elif stick==2:
                        print("You chose to roll again. Producing numbers now.")
                    else:
                        print("I'm sorry, that's not a valid command.")
    
    def computeroll(playeraddedrolls,playagain,playagain1or2,computeraddedrolls):
    while computeroll<17:
        die3=random.randint(1,6)
        die4=random.randint(1,6)
    
        print("The comoputer rolled ",die3,"and ",die4,".")
        computeraddedrolls=die3+die4
        if playeraddedrolls>21:
            winningtally(playeraddedrolls,computeraddedrolls,playagain,playagain1or2)
        else:
            if computeraddedrolls<17:
                print("The computer chose to roll again!")
            elif computeraddedrolls>21:
                print("The computer rolled over 21, you win by default!")
                winningtally(playeraddedrolls,computeraddedrolls,playagain,playagain1or2)
            else:
                print("Overall, the computer scored ", computeraddedrolls,".")
                winningtally(playeraddedrolls,computeraddedrolls,playagain,playagain1or2)
    
    def winningtally(PAR,CAR,playagain,playagain1or2):
    if playeraddedrolls>21 or computeraddedrolls>playeraddedrolls:
        print("I have added the computers win to the tally. Here is the new set of wins:")
        append(computer)
        print(winner)
        playagain(PAR,CAR,playagain,playagain1or2)
    elif computeraddedrolls>21 or playeraddedrolls>computeraddedrolls:
        print("I have added your win to the tally. Here is the new set of wins:")
        append(player)
        print(winner)
        playagain(PAR,CAR,playagain,playagain1or2)
    
    def playagain(PAR,CAR,playagain,playagain1or2):
    while playagain1or2<1 or playagain1or2>2:
        playagain1or2=int(input("Press 1 to play again, or 2 to view the final result."))
        if playagain1or2==1:
            print("Okay, rerunning...")
            return
        elif playagain1or2==2:
            computerwins=(winner).count(computer)
            playerwins=(winner).count(player)
            if computerwins>playerwins:
                print("Sorry, the computer won. Better luck next time!")
            else:
                print("Congratulations, you won! Thank you for playing!")
        else:
            print("I'm sorry, ",playagain1or2," is not a valid command.")
    
    
    
    playeraddrolls=True
    playeraddedrolls=2
    computeraddedrolls=2
    playagain1or2=0
    playagain=True    
    while playagain==True:
    stick=0
    addedrolls=3
    diceroll(addedrolls,stick,playagain,playagain1or2,playeraddedrolls,computeraddedrolls,playeraddrolls)
    


Answer: Assuming that your code works as expressed (it's a lot of code to check), your
problem is that `False < 21 == True`.

Here is your `while` condition:

    
    
    while playeraddedrolls<21 or playeraddrolls is True:
    

Remember that `or` short-circuits. It only needs one thing to be true for a
whole string of `x or y or z or...` to be true logically, so as soon as the
first thing checked is true, `or` stops looking.

Since you are setting `playeraddedrolls = False` to break out of this loop,
the check becomes `False < 21`, which is true and short-circuits.

# Practical Solutions -- Alternate Conditions

Rather than setting `playeraddedrolls = False` and _implicitly_ breaking, you
could explicitly add `break` there. However, this isn't recommended because
`break` statements can quite easily get buried and thus can be difficult to
debug.

Perhaps better still would be to change the while condition to this:

    
    
    while 0 < playeraddedrolls < 21:
    

This allows you to set `playeraddedrolls = -1` for the desired implicit break.

# But WHY does Python do this?

As explained in my comment, booleans are a subclass of integers, because True
and False can be thought of as special cases of the numbers 0 and 1. This lets
you do some perhaps surprising numerical things to booleans.

    
    
    >>> True + False
    1
    >>> True - False
    1
    >>> True * False
    0
    >>> True % False
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    ZeroDivisionError: integer division or modulo by zero
    >>> False % True
    0
    

You can see above that all the numerical operators simply coerce the booleans
to 0 and 1 and function quite happily.

