Python while with two conditions: and or or

Python while with two conditions: and or or

TLDR at bottom.

First off, while loops run if the following condition is true, so

DieOne != 6 or DieTwo != 6:

must return true when simplified, for the while funtion to run

The and operator returns true if both conditions are true, so the while loop will only run when it is True and True.

So the following wont run if either of the dice rolled a 6 for example:

while DieOne != 6 and DieTwo != 6:

If DieOne rolled a 4 and DieTwo rolled a 6, the while loop wont run because DieOne != 6 is true, and DieTwo != 6 is false. I put this train of thought into code below.

while DieOne != 6 and DieTwo != 6:
while True and False:
while False: #So it wont run because it is false

The or operator works differently, the or operator returns true when one of the conditions is true, so the while loop will run when it is True or True, True or False, or _False or True.
So

while DieOne != 6 or DieTwo != 6:

will run if only either dice rolled a six. For example:

If DieOne rolled a 4 and DieTwo rolled a 6, the while loop will run because DieOne != 6 is true, and DieTwo != 6 is false. I put this train of thought into code below.

while DieOne != 6 or DieTwo != 6:
while True or False:
while True: #So it will run because it is true

TLDR/Review:

while True: #Will run
while False: #Wont run

And:

while True and True: #Will run
while True and False: #Wont run
while False and True: #Wont run
while False and False: #Wont run

Or:

while True or True: #Will run
while True or False: #Will run
while False or True: #Will run
while False or False: #Wont run

What you need is Not instead of !=.

try this:

while not (DieOne == 6 or DieTwo == 6):

Python while with two conditions: and or or

while DieOne != 6:
   if DieTwo != 6:
      break
   num = num + 1
   DieOne = random.randint(1, 6)
   DieTwo = random.randint(1, 6)
   print(DieOne)
   print(DieTwo)
   print()
   if (DieOne == 6) and (DieTwo == 6):
      num = str(num)
      print(You got double 6s in  + num +  tries!)
      print()
      break

Leave a Reply

Your email address will not be published.