Home > other >  call list not searching all the way through in python
call list not searching all the way through in python

Time:04-26

I have this code:

class Junior_devs:
    entry_list = []
    raise_amount = 1.04
    sum = 0
    def __init__(self, first, last, pay, email):
        self.first = first
        self.last = last
        self.pay = pay
        self.email = email
    def apply_raise(self):
        if self in Junior_devs.entry_list:
           self.pay = (int(self.pay) * Junior_devs.raise_amount)
        else:
            self.pay = (int(self.pay) * Senior_devs.raise_amount)
    @classmethod
    def add_employee(cls, string):
        first, last, pay, email = string.split(" ")
        emp = cls(first, last, int(pay), email)
        cls.entry_list.append(emp)
        return emp
    @classmethod
    def sumOfpay(cls):
        sum = 0
        for x in cls.entry_list:
            dicti = x.__dict__
            sum  = int(dicti["pay"])
        return sum
    def change_level(self):
        if self in Junior_devs.entry_list:
            Junior_devs.entry_list.remove(self)
            Senior_devs.entry_list.append(self)
        elif self in Senior_devs.entry_list:
            Senior_devs.entry_list.remove(self)
            Junior_devs.entry_list.append(self)
class Senior_devs(Junior_devs):
    entry_list = []
    raise_amount = 1.10


emp1 = Junior_devs.add_employee("imri peretz 20000 [email protected]")
emp3 = Junior_devs.add_employee("tray belfort 10000 [email protected]")
emp2 = Senior_devs.add_employee("Test User 1000 [email protected]")
emp4 = Senior_devs.add_employee("jordan belfort 2000 [email protected]")


print("Juniors: ")
for x in Junior_devs.entry_list:
    print(x.__dict__)
print("Seniors: ")
for x in Senior_devs.entry_list:
    print(x.__dict__)
print("---------------")
for x in Junior_devs.entry_list:
    Junior_devs.change_level(x)
print("Juniors: ")
for x in Junior_devs.entry_list:
    print(x.__dict__)
print("Seniors: ")
for x in Senior_devs.entry_list:
    print(x.__dict__)

when i use the change_level function in order to switch an instance from a junior to a senior they all switch but there is always 1 instance that dosent switch. before using function:

Juniors: 
{'first': 'imri', 'last': 'peretz', 'pay': 20000, 'email': '[email protected]'}
{'first': 'tray', 'last': 'belfort', 'pay': 10000, 'email': '[email protected]'}
Seniors:
{'first': 'Test', 'last': 'User', 'pay': 1000, 'email': '[email protected]'}
{'first': 'jordan', 'last': 'belfort', 'pay': 2000, 'email': '[email protected]'}

after using:

Juniors:
{'first': 'tray', 'last': 'belfort', 'pay': 10000, 'email': '[email protected]'}
Seniors:
{'first': 'Test', 'last': 'User', 'pay': 1000, 'email': '[email protected]'}
{'first': 'jordan', 'last': 'belfort', 'pay': 2000, 'email': '[email protected]'}
{'first': 'imri', 'last': 'peretz', 'pay': 20000, 'email': '[email protected]'}

the last instance in Juniors should also move to Seniors. that is what i have trouble with because the way im looping through the entry list of the class should loop through the entire list but i cant find a reason as to why its stopping at the last element. would love some help on this.

CodePudding user response:

All problem makes remove() which you run on iterated list.

When you remove element from list then next element is moved on its place and later for-loop skips this element. If you would have more elements then it would skip all "even" elements (second, fourth, 6th, 8th, etc.)

You should first append all elements to Senior and later remove all elements from Junion.

OR you should work on copy of this list

for x in Junior_devs.entry_list.copy():  # <-- work on copy of list
    Junior_devs.change_level(x)
  • Related