lundi 2 novembre 2020

How to conditionally adapt if statement in python [closed]

I have a function called do_something. It takes 3 input variables a, b, c. The default values for these 3 variables are all None. I want to do something when meeting one of the 3 conditions: a == 1, b == 2 and c == 3. Below is the code I wrote, but there has to be a better way to code it. In my real case, the function takes 8 input variables and this becomes a disaster.

def do_something(a=None, b=None, c=None):
    if (a is None) and (b is not None) and (c is not None):
        if (b == 2) and (c == 3):
            do something
    elif (b is None) and (a is not None) and (c is not None):
        if (a == 1) and (c ==3):
            do something
    elif (c is None) and (a is not None) and (c is not None):
        if (a == 1) and (b == 2):
            do something
    elif (a is None) and (b is None) and (c is not None):
        if c == 3:
            do something
    elif (a is None) and (b is not None) and (c is None):
        if b == 2:
            do something
    elif (a is not None) and (b is None) and (c is None):
        if a == 1:
            do something
    elif (a is not None) and (b is not None) and (c is not None):
        if (a == 1) and (b == 2) and (c == 3):
            do something
    else:
        raise ValueError('No input value')

Aucun commentaire:

Enregistrer un commentaire