mardi 20 novembre 2018

How to check if one or two integers are -1, among four integer variables in total, Python3?

I have 4 integer variables

r1, r2, c1, c2

They can be any integer values. I want to find out which of them is/are -1.

The rule is r1 and r2 cannot be -1 at the same time; c1 and c2 cannot be -1 at the same time.

I can use if statement, but it can be cumbersome:

if r1 == -1:        
    if c1 == -1:
        # do action a
    elif c2 == -1:
        # do action b
    else:  # c1 and c2 are both not -1
        # do action c
elif r2 == -1:
    if c1 == -1:
        # do action d
    elif c2 == -1:
        # do action e
    else:  # c1 and c2 are both not -1
        # do action f 
else:  # none of the four integers is -1.
    # do action g

Could you please show me some more elegant and efficient algorithm?

Thanks.

Aucun commentaire:

Enregistrer un commentaire