Bottom Page

• 0 Vote(s) - 0 Average
• 1
• 2
• 3
• 4
• 5
 Why doesn't my loop work correctly? (problem with a break statement) steckinreinhart619 Programmer named Tim Posts: 5 Threads: 2 Joined: Jun 2019 Reputation: 0 Likes received: 1 #1 Jun-09-2019, 02:08 PM 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()``` metulburr The Castle of aaarrrrggh Posts: 4,394 Threads: 280 Joined: Sep 2016 Reputation: 128 Likes received: 1117 #2 Jun-09-2019, 02:56 PM Every time you evaluate, you are looping the entire list. Apple1 which has the same color as banana2, is executed per the elif condition. ``````Output:apple = {'name': 'Apple1', 'color': 'red', 'price': 5} banana = {'name': 'Banana2', 'color': 'red', 'price': 3} changing name of Banana2 to Apple1 based on color ``````I usually put prints around to quickly identify control With break: ``` def evaluate(self): for a in Apple_List: print(f'apple = {a.__dict__}') print(f'banana = {self.__dict__}') if self.price==a.price: print(f'changing name of {self.name} to {a.name} based on price') self.name=a.name break elif self.color==a.color: print(f'changing name of {self.name} to {a.name} based on color') self.name=a.name break else: print('else clause') print(self.name)`````````Output:metulburr@ubuntu:~\$ python3.6 test3.py apple = {'name': 'Apple1', 'color': 'red', 'price': 5} banana = {'name': 'Banana1', 'color': 'yellow', 'price': 5} changing name of Banana1 to Apple1 based on price Apple1 apple = {'name': 'Apple1', 'color': 'red', 'price': 5} banana = {'name': 'Banana2', 'color': 'red', 'price': 3} changing name of Banana2 to Apple1 based on color Apple1 apple = {'name': 'Apple1', 'color': 'red', 'price': 5} banana = {'name': 'Banana3', 'color': 'green', 'price': 7} else clause apple = {'name': 'Apple2', 'color': 'yellow', 'price': 3} banana = {'name': 'Banana3', 'color': 'green', 'price': 7} else clause apple = {'name': 'Apple3', 'color': 'green', 'price': 10} banana = {'name': 'Banana3', 'color': 'green', 'price': 7} changing name of Banana3 to Apple3 based on color Apple3 ``````commenting out the elif break makes it look as you expected because ut changes banana name to apples for the next evaluation. ``````Output:metulburr@ubuntu:~\$ python3.6 test3.py apple = {'name': 'Apple1', 'color': 'red', 'price': 5} banana = {'name': 'Banana1', 'color': 'yellow', 'price': 5} changing name of Banana1 to Apple1 based on price Apple1 apple = {'name': 'Apple1', 'color': 'red', 'price': 5} banana = {'name': 'Banana2', 'color': 'red', 'price': 3} changing name of Banana2 to Apple1 based on color apple = {'name': 'Apple2', 'color': 'yellow', 'price': 3} banana = {'name': 'Apple1', 'color': 'red', 'price': 3} changing name of Apple1 to Apple2 based on price Apple2 apple = {'name': 'Apple1', 'color': 'red', 'price': 5} banana = {'name': 'Banana3', 'color': 'green', 'price': 7} else clause apple = {'name': 'Apple2', 'color': 'yellow', 'price': 3} banana = {'name': 'Banana3', 'color': 'green', 'price': 7} else clause apple = {'name': 'Apple3', 'color': 'green', 'price': 10} banana = {'name': 'Banana3', 'color': 'green', 'price': 7} changing name of Banana3 to Apple3 based on color Apple3 `````` Recommended Tutorials: BBcode, Forum Rules and Instructions, How to ask smart questions, the Basics, Classes, Python Gotchas steckinreinhart619 Programmer named Tim Posts: 5 Threads: 2 Joined: Jun 2019 Reputation: 0 Likes received: 1 #3 Jun-11-2019, 10:02 AM (Jun-09-2019, 02:08 PM)steckinreinhart619 Wrote: 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()``` Thanks a lot for your answer metulburr! I figured out the problem thanks to your help. The tip of putting prints around each loop is a really good idea. Thumbs up! metulburr likes this post « Next Oldest | Next Newest »

Top Page

 Possibly Related Threads... Thread Author Replies Views Last Post How to print the map correctly janaki26794 0 110 Aug-06-2019, 11:00 PM Last Post: janaki26794 Asyncio StreamReader read method doesn't respect timeout when using SSL dukadahake 0 79 Jul-24-2019, 11:55 AM Last Post: dukadahake Why does modifying a list in a for loop not seem to work? umut3806 2 170 Jul-22-2019, 08:25 PM Last Post: umut3806 Am I running this correctly? jibarra 0 116 Jul-18-2019, 07:00 PM Last Post: jibarra Pynput doesn't recognize shift button and special characters VirtualDreamer 0 104 Jul-17-2019, 11:55 AM Last Post: VirtualDreamer Cant get grade part of code to work correctly Expel 5 226 Jul-10-2019, 05:09 AM Last Post: perfringo Nested while loop problem + turtle DreamingInsanity 3 165 Jul-06-2019, 02:01 PM Last Post: DreamingInsanity Python if statement docx wount work Selfiatus1 5 224 Jun-07-2019, 11:44 PM Last Post: Larz60+ diagnosing break not working Skaperen 3 324 May-25-2019, 07:00 AM Last Post: Skaperen Python Script Spawned by Cron or Systemd doesn't write files..? johnnyrobot 2 225 May-24-2019, 07:04 PM Last Post: Larz60+

Forum Jump:

Users browsing this thread: 1 Guest(s)