lundi 25 octobre 2021

How to reduce if..statements for similar operations by bool judgment in python?

The example codes are like below:

class triangle:
    def __init__(self,a,b,c):
        self.a = a
        self.b = b
        self.c = c

    def addA(self):
        total = self.a + 3
        return total

    def addB(self):
        total2 = self.b + 6
        return total2    

    def addC(self):
        total3 = self.c + 6
        return total3

    def addAll(self):
        total4 = self.a + self.b + self.c
        return total4


t = triangle(5,6,7)


if t.a !=5:
    print(t.addB())
    print(t.addC())

if t.b != 6:
    print(t.addA())
    print(t.addC())
    
if t.c != 7:
    print(t.addA())
    print(t.addB())

if t.a == 5 and t.b == 6 and t.c ==7:
    print(t.addAll())

The key problem is how to skip one method when one attribute judgement is false.

I am wondering if there is anyway to reduce if statement.

Aucun commentaire:

Enregistrer un commentaire