Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python 'while' with two conditions: "and" or "or"

This is a very simple dice roll program that keeps rolling two dice until it gets double sixes. So my while statement is structured as:

while DieOne != 6 and DieTwo != 6:

For some reason, the program ends as soon as DieOne gets a six. DieTwo is not considered at all.

But if I change the and to an or in the while statement, the program functions perfectly. This doesn't make sense to me.

import random
print('How many times before double 6s?')
num=0
DieOne = 0
DieTwo = 0

while DieOne != 6 or DieTwo != 6:
    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
like image 299
ghulseman Avatar asked Jan 12 '19 19:01

ghulseman


People also ask

Can I have 2 conditions in while loop Python?

Python While Loop Multiple Conditions. To combine two conditional expressions into one while loop, you'll need to use logical operators. This tells Python how you want all of your conditional expressions to be evaluated as a whole.

Can a while loop have two conditions?

As seen on line 4 the while loop has two conditions, one using the AND operator and the other using the OR operator. Note: The AND condition must be fulfilled for the loop to run. However, if either of the conditions on the OR side of the operator returns true , the loop will run.

Can we use && in while loop?

Suppose in a while loop, you have two conditions, and any one needs to be true to proceed to the body, then in that case you can use the || operator between those two conditions, and in case you want both to be true, you can use && operator. By using if statements and logical operators such as &&, 11,!

Can we use or operator in while loop in Python?

while loops are another example of Boolean context where you can use the Python or operator.


2 Answers

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 won't 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 won't 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 won't 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: #Won't run

And:

while True and True: #Will run
while True and False: #Won't run
while False and True: #Won't run
while False and False: #Won't run

Or:

while True or True: #Will run
while True or False: #Will run
while False or True: #Will run
while False or False: #Won't run
like image 187
Aaron D. Rodriguez Avatar answered Oct 14 '22 01:10

Aaron D. Rodriguez


What you need is Not instead of !=.

try this:

while not (DieOne == 6 or DieTwo == 6):
like image 24
gaurav ujjain Avatar answered Oct 14 '22 00:10

gaurav ujjain