Python Forum
Why doesn't my loop work correctly? (problem with a break statement)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why doesn't my loop work correctly? (problem with a break statement)
#1
Hey guys I'm having some trouble with a loop 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).

I wanted to define a method for the banana class which iterates over a list of apples and 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).

Now, whenever I try to run that code with the 3 Banana objects, Banana1 and 3 are assigned to the correct Apple,but Banana2 is assigned to Apple1 even though it should be assigned to Apple 2. I experimented a bit and found out that when I remove the break statement at the end of my elif condition which checks for the matching color all Bananas are assigned to the correct Apple. However, since I would like to set up another elif condition I cannot delete that break statement, right?
If anyone could help me to understand what is wrong with my code I would be very grateful. 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)
A3=Apple("Apple3", "green", 10 )

Apple_List=[A1, A2, A3]

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
                break

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

        print(self.name)


B1=Banana("Banana1","yellow", 5)
B2=Banana("Banana2", "red", 3)
B3=Banana("Banana3", "green", 7)

B1.evaluate()
B2.evaluate()
B3.evaluate()
Reply


Messages In This Thread
Why doesn't my loop work correctly? (problem with a break statement) - by steckinreinhart619 - Jun-09-2019, 02:08 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  hi need help to make this code work correctly atulkul1985 5 783 Nov-20-2023, 04:38 PM
Last Post: deanhystad
Photo Python code: While loop with if statement HAMOUDA 1 576 Sep-18-2023, 11:18 AM
Last Post: deanhystad
  While Loop Problem Benno2805 1 580 Sep-06-2023, 04:51 PM
Last Post: deanhystad
  Why doesn't calling a parent constructor work with arbitrary keyword arguments? PurposefulCoder 4 938 Jun-24-2023, 02:14 PM
Last Post: deanhystad
  While Loop Does Not Work Properly mactron 4 916 Jun-22-2023, 01:04 AM
Last Post: mactron
  Multiply and Addition in the same loop statement with logic. joelraj 2 1,038 Feb-02-2023, 04:33 AM
Last Post: deanhystad
  Why doesn't this code work? What is wrong with path? Melcu54 7 1,785 Jan-29-2023, 06:24 PM
Last Post: Melcu54
  color code doesn't work harryvl 1 889 Dec-29-2022, 08:59 PM
Last Post: deanhystad
  If statement not working correctly? MrKnd94 2 829 Nov-16-2022, 02:49 AM
Last Post: deanhystad
  Code won't break While loop or go back to the input? MrKnd94 2 952 Oct-26-2022, 10:10 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020