mercredi 15 janvier 2020

Python 3: Using for loop & if statement and .append, how to perform some operations on different elements using if statement without overwriting?

I am trying to use for loop and if statements on the following:

class equipment:
    def __init__(self, name, location, model):
        self.name = name
        self.location = location
        self.model=model

class device(equipment):
    def __init__(self, name, location,model):
        super().__init__(name, location,model)


device1 = device("device1 ", (5, 2), 'D')
device2 = device("device2", (10, 2), 'T')
device3 = device("device3", (15, 3), 'M')
device4 = device("device4", (5, 5), 'unknown')
eff= 0.5
fi=100
value=[]
all_devices = [device1, device2, device3, device4]
for n in range(0, 4):
    if all_devices[n].model=='D':
        eff= eff + 0.3
        fo=fi*eff
        value.append(fo+120)
    elif all_devices[n].model == 'T':
        eff= eff +0.10
        fo=fi*eff
        value.append(fo+120)
    else:
        eff= eff +0.25
        fo=fi * eff
        value.append(fo+120)
    output=value
print(output)

But the output is coming like that

[200.0, 210.0, 235.0, 260.0]

While I am expecting something like that

[200, 180 , 195 , 195]

So, how to perform these operations on different elements using if statements without overwriting?

Aucun commentaire:

Enregistrer un commentaire