vendredi 18 décembre 2020

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

I have three numbers M1, M2, M3 and range (a, b). I need find the maximum number in the range or print "Error", if at least one number is not in range (a, b). I must use if-else statment only. My code is below:

M1 = 101
M2 = 102
M3 = 103

a = 100
b = 1000
if a<M1<b:
    if M2<=M1 and M3<=M1:
        print(M1)
        
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.

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

Aucun commentaire:

Enregistrer un commentaire