jeudi 15 août 2019

How do I condense the loop and if else

Is there a better or shorter way to write this code?

def business_role_code_to_name(x):
    y = []

    for position, code in enumerate(x):
        if position == 0 and code == 1.0: 
            y.append("Owner") 
        elif position == 1 and code == 1.0: 
            y.append("Manager")
        elif position == 2 and code == 1.0:
            y.append("Employee")
        elif position == 3 and code == 1.0:
            y.append("Other")
    return y

assert business_role_code_to_name([1.0, 1.0, 1.0, 0.0]) == ['Owner', 'Manager', 'Employee']
assert business_role_code_to_name([0.0, 1.0, 0.0, 1.0]) == ['Manager', 'Other']

I am new to programming, I think there is a better way to write this code. Thanks!

Aucun commentaire:

Enregistrer un commentaire