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)
#2
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:
Reply


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

Possibly Related Threads…
Thread Author Replies Views Last Post
  hi need help to make this code work correctly atulkul1985 5 800 Nov-20-2023, 04:38 PM
Last Post: deanhystad
Photo Python code: While loop with if statement HAMOUDA 1 582 Sep-18-2023, 11:18 AM
Last Post: deanhystad
  While Loop Problem Benno2805 1 586 Sep-06-2023, 04:51 PM
Last Post: deanhystad
  Why doesn't calling a parent constructor work with arbitrary keyword arguments? PurposefulCoder 4 955 Jun-24-2023, 02:14 PM
Last Post: deanhystad
  While Loop Does Not Work Properly mactron 4 926 Jun-22-2023, 01:04 AM
Last Post: mactron
  Multiply and Addition in the same loop statement with logic. joelraj 2 1,044 Feb-02-2023, 04:33 AM
Last Post: deanhystad
  Why doesn't this code work? What is wrong with path? Melcu54 7 1,809 Jan-29-2023, 06:24 PM
Last Post: Melcu54
  color code doesn't work harryvl 1 896 Dec-29-2022, 08:59 PM
Last Post: deanhystad
  If statement not working correctly? MrKnd94 2 843 Nov-16-2022, 02:49 AM
Last Post: deanhystad
  Code won't break While loop or go back to the input? MrKnd94 2 965 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