vendredi 18 juin 2021

Refactoring multiple if / elif statements

I would like to replace the multiple if/elif statements with something more pythonic (preferably a design pattern):

def calculate_coords(self, num_of_chars):
    if num_of_chars <  20:
        return int(num_of_chars * 6.8 )
    elif num_of_chars <  30:
        return int(num_of_chars * 6.7 )
    elif num_of_chars <  40:
        return int(num_of_chars * 6.25 )
    elif num_of_chars <  50:
        return int(num_of_chars * 6.35 )
    elif num_of_chars <  60:
        return int(num_of_chars * 6.2 )
    elif num_of_chars <  70:
        return int(num_of_chars * 6.15 )
    elif num_of_chars <  80:
        return int(num_of_chars * 6.2 )
    elif num_of_chars <  90:
        return int(num_of_chars * 6.05 )
    elif num_of_chars <  100:
        return int(num_of_chars * 6.05 )
    elif num_of_chars <  110:
        return int(num_of_chars * 6)
    elif num_of_chars <  120:
        return int(num_of_chars * 5.9 )
    elif num_of_chars <  130:
        return int(num_of_chars * 5.85 )
    else:
        return int(num_of_chars * 5.85 )

Aucun commentaire:

Enregistrer un commentaire