Nick
Nick

Reputation: 1106

How to union three if-statement's into one?

I have three numbers M1, M2, M3 and range (a, b). I need 1) find the maximum number in the range or 2) print "Error", if at least one number is not in range (a, b). And 3) I need use the if-statement which was given by an instructor:

M1 = 101
M2 = 102
M3 = 103

a = 100
b = 1000
if a<M1<b:
    if M2<=M1 and M3<=M1:
        print(M1)
        

My first step was add analogical if-statements for the M2, M3. I have typed:

if a<M2<b:
    if M1<=M2 and M3<=M2:
        print(M2)
        
if a<M3<b:
    if M1<=M3 and M2<=M3:
        print(M3)

One can see that I don't code the second condition here. I have tried to union my three if-statement in one:

M1 = 99
M2 = 102
M3 = 103
a = 100
b = 1000
if a<M1<b:
    if M2<=M1 and M3<=M1:
        print(M1)
elif a<M2<b:
    if M1<=M2 and M3<=M2:
        print(M2)
elif a<M3<b:
    if M1<=M3 and M2<=M3:
        print(M3)        
else:
    print("Error")

but the console line is empty.

My second idea looks like for M1:

if a<M1<b:
        if M2<=M1 and M3<=M1:
            print(M1)
else:
            print("Error")

And than add the same construction for M2, M3. But in the worst case I will see thee times Error, if all numbers are not in range.

Question. How to union three if-statements into one?

Upvotes: 2

Views: 336

Answers (3)

Tom Myddeltyn
Tom Myddeltyn

Reputation: 1375

Since it wasn't super clear in the question. I've included two solutions for you. The first one strictly checks M1, M2, and M3 to ALL be in range. The second only finds max of those in the range.

M1 = 99
M2 = 102
M3 = 103
a = 100
b = 1000

#This solution makes sure all of the elements M1, M2 AND M3 are in range then does max
# iff they are all in the range of a> x <b
if not((M1 > a and M1 < b) and (M2 > a and M2 < b) and (M3 > a and M3 <b)):
    #If all are not in the range: ERROR
    print("Error")
elif M2<=M1 and M3<=M1:
    #is M1 max of M1 vs M2 and M1 vs M3?
    print(M1)
elif M3<=M2:
    #is M2 max of MM2 vs M3?
    print(M2)
else:
    #Only M3 is left so must be max.
    print(M3)        

#This one will check for max of M1,M2,M3 which are in range of a > x > b if 
# one is out of range (say M1) it will not be checked as maximum.
if not((M1 > a and M1 < b) or (M2 > a and M2 < b) or (M3 > a and M3 <b)):
    print("Error1")
elif (M1 > a and M1 < b) and (M2<=a or M2>=b or M1>=M2) and (M3<=a or M3>=b or M1>=M3):
    #Validate M2 AND validate M3 along with max M2 vs M1 AND validate M3 along with M3 vs M1
    print(M1)
elif  (M2 > a and M2 < b) and (M3<=a or M3>=b or M3<=M2):
    #Validate M2 AND validate M3 along with max M3 vs M2 we don't need to check M1 again.
    print(M2)
elif (M3 > a and M3 <b):
    #only M3 is left, so just validate in range.
    print(M3)
else:
    #Not sure this condition should ever happen, but trap it.
    print("Error2")

Upvotes: 2

Gustav Rasmussen
Gustav Rasmussen

Reputation: 3961

Using the min and max function together with named expressions:

M1 = 101
M2 = 102
M3 = 103

a = 100
b = 1000

if a < (M_max := max([M1, M2, M3])) < b and a < (M_min := min([M1, M2, M3])) < b:
    print(M_max)
else:
    print("Error")

Result:

103

Without named expressions:

M_max = max([M1, M2, M3])
M_min = min([M1, M2, M3])

if a < M_max < b and a < M_min < b:
    print(M_max)
else:
    print("Error")

Without built-in functions (min and max):

if a < M1 < b and a < M2 < b and a < M3 < b:
    if M1 >= M2 >= M3:
        print(M1)
    elif M2 >= M3:
        print(M2)
    else:
        print(M3)
else:
    print("Error")

Upvotes: 1

Raz K
Raz K

Reputation: 167

You can try this to get the maximum between M1, M2 and M3:

M1 = 100
M2 = 101
M3 = 102
a = 100
b = 1000

if (M1 < a or M2 < a or M3 < a) or (M1 > b or M2 > b or M3 > b):
    print('Error')
else:
    if M1 > M2:
        if M1 > M3:
            print(M1)
        else:
            print(M3)
    else:
        if M2 > M3:
            print(M2)
        else:
            print(M3)

Upvotes: 0

Related Questions