Alberto Gonzalez
Alberto Gonzalez

Reputation: 33

Is there a way to reduce the if-statements? Python

Im writing a code, at the beginning i need to establish the attributes of my class, but those depend on the value of sueldo, so i wrote some if-statements to do that. Is there some way i can reduce them?

class sueldobruto:

    def __init__(self, sueldo):
        self.sueldo = sueldo
        if 0.01 <= self.sueldo <= 19.03:
            self.limiteinferior = 0.01
            self.limitesuperior = 19.03
            self.cuotafija = 0
            self.porcentaje = 0.0192
        elif 19.04 <= self.sueldo <= 161.52:
            self.limiteinferior = 19.04
            self.limitesuperior = 161.52
            self.cuotafija = 0.37
            self.porcentaje = 0.064
        elif 161.53 <= self.sueldo <= 283.86:
            self.limiteinferior = 161.53
            self.limitesuperior = 283.86
            self.cuotafija = 9.48
            self.porcentaje = 0.1088
        elif 283.87 <= self.sueldo <= 329.97:
            self.limiteinferior = 283.87
            self.limitesuperior = 329.97
            self.cuotafija = 22.79
            self.porcentaje = 0.16
        elif 329.98 <= self.sueldo <= 395.06:
            self.limiteinferior = 329.98
            self.limitesuperior = 395.06
            self.cuotafija = 30.17
            self.porcentaje = 0.1792
        elif 395.07 <= self.sueldo <= 796.79:
            self.limiteinferior = 395.07
            self.limitesuperior = 796.79
            self.cuotafija = 41.84
            self.porcentaje = 0.2136
        elif 796.8 <= self.sueldo <= 1255.85:
            self.limiteinferior = 796.8
            self.limitesuperior = 1255.85
            self.cuotafija = 127.65
            self.porcentaje = 0.2352

Upvotes: 1

Views: 137

Answers (2)

Avinash Dalvi
Avinash Dalvi

Reputation: 9301

This is one of approach you can to reduce number of if :

sample_array = {
    "0.01-19.03" : [{
              "limiteinferior" : 0.01,
              "limitesuperior" : 19.03,
              "cuotafija" : 0,
              "porcentaje" : 0.0192
            }
        ],
    
    "19.03-161.52" : [{
              "limiteinferior" : 19.03,
              "limitesuperior" : 161.52,
              "cuotafija" : 0.37,
              "porcentaje" : 0.064
            }
        ],
    "161.52-283.86" : [{
              "limiteinferior" : 161.52,
              "limitesuperior" : 283.86,
              "cuotafija" : 9.48,
              "porcentaje" : 0.1088
            }
        ]
}

finder = 18
limiteinferior = 0
limitesuperior = 0
cuotafija = 0
porcentaje = 0

for key, value in sample_array.items():
    split_range = key.split("-")
    #print(split_range)
    if float(split_range[0]) <=  finder and float(split_range[1]) >= finder:
        limiteinferior = sample_array.get(key)[0].get("limiteinferior")
        limitesuperior = sample_array.get(key)[0].get("limitesuperior")
        cuotafija = sample_array.get(key)[0].get("cuotafija")
        porcentaje = sample_array.get(key)[0].get("porcentaje")
        
print(limiteinferior)
print(limitesuperior)

print(cuotafija)
print(porcentaje)

This is one of approach which I tried.

Upvotes: 1

Pavan kumar
Pavan kumar

Reputation: 515

i used this to reduce if statement complexity. i just assigned limiteinferior, limitesuperior not assigned cuotafija, porcentaje because idon't know how they are realted to given value if you know you can just edit the code inside forloop

class sueldobruto:
    def __init__(self, sueldo):
        self.sueldo = sueldo
        list = [0.01, 19.03,161.52,283.86,329.97, 395.06, 796.79, 1255.85]

        if self.sueldo < 0.01:
            pass
        for x in list[1:]:
            if self.sueldo <= x:
                self.limiteinferior = list[list.index(x) - 1] + 0.01
                self.limitesuperior = x

Upvotes: 1

Related Questions