jeudi 6 juin 2019

Why doesn`t my if statement work correctly?

Hey guys I'm having some trouble with an if-statement in python I`m working on. I simplified the code for this question since the core of the problem stays the same and it makes it easier to understand: So lets say I create two classes, one for Apples and one for Bananas. Their attributes are basically the same (name, color and price).

class Apple:                                   
    def __init__(self, name, color, price):
        self.name = name
        self.color = color
        self.price = price

A1=Apple("Apple1", "red", 5)
A2=Apple("Apple2", "yellow", 3)

Apple_List=[A1, A2]


class Banana:
    def __init__(self, name, color, price):
        self.name = name
        self.color = color
        self.price= price

B1=Banana("Banana1","yellow", 5)
B2=Banana("Banana2", "brown", 1)

Banana_List=[B1,B2]

So far so good. I wanted to define a method for the banana class which should check first whether the price of the banana is the same as for one of the apple objects. If that is the case, the banana should take over that apple's name (again, it doesnt make any sense in this code, but its relevant for the actual project I'm working on). In the second place, the method is supposed to check whether the banana has the same color as one of the apple objects and then take over that apples name (I used an elif statement for that one). Here's what the method looks like:

def evaluate(self):
    for a in Apple_List:
        if self.price==a.price:
            self.name=a.name

        elif self.color==a.color:
            self.name=a.name

        else:
            pass

    print(self.name)

Now, whenever I try to run that code with Banana1, I get "Apple2" as a result, even though Banana1 has the same price as Apple1 and should therefore be named "Apple1". It looks like the elif statement is executed before the if statement, since Banana1 has the same color as Apple2 and takes over its name. But shouldn't the if statement have priority over the elif statement? Sorry if there is an obvious solution for this problem, I'm very new to python programming. Thanks in advance for your help! Heres the full code:

class Apple:                                   
    def __init__(self, name, color, price):
        self.name = name
        self.color = color
        self.price = price

A1=Apple("Apple1", "red", 5)
A2=Apple("Apple2", "yellow", 3)

Apple_List=[A1, A2]

class Banana:
    def __init__(self, name, color, price):
        self.name = name
        self.color = color
        self.price= price

    def evaluate(self):
        for a in Apple_List:
            if self.price==a.price:
                self.name=a.name

            elif self.color==a.color:
                self.name=a.name

            else:
                pass

        print(self.name)



B1=Banana("Banana1","yellow", 5)
B2=Banana("Banana2", "brown", 1)

Banana_List=[B1,B2]

B1.evaluate()

Aucun commentaire:

Enregistrer un commentaire