Class with adding object with limited number until full

I am trying to create a class that can add different combinations up to 10 as I add. What I am struggling with is the adding of the different combinations into a variable that I can query. Here is the current code I have:

class Plugboard:
    def __init__(self):
        self.__head = 0
    
    def add(self, item):
        __head = 0
        if __head >= 10:
            leads = item.append()
            __head += 1
        return leads

The information I add as an input is the following:

plugboard = Plugboard()
plugboard.add("SZ")
plugboard.add("AG")
plugboard.add("CD")

And would like to query the variable to see what the different plugboard combinations are. Any guidance will be appreciated.

1 answer

  • answered 2021-06-23 07:38 leaf_yakitori

    you need use self.leads, not leads.

    code:

    class Plugboard:
        def __init__(self):
            self.__head = 0
            self.leads = []
        
        def add(self, item):
            if self.__head >= 10:
                print("leads already got 10 items")
            elif item in self.leads:
                print(f"leads already have this item: {item}")
            else:
                self.leads.append(item)
                self.__head += 1
            return self.leads
    
    plugboard = Plugboard()
    print(plugboard.add("SZ"))
    print(plugboard.add("AG"))
    print(plugboard.add("CD"))
    
    print(plugboard.add("CD"))
    
    print(plugboard.add("4"))
    print(plugboard.add("5"))
    print(plugboard.add("6"))
    print(plugboard.add("7"))
    print(plugboard.add("8"))
    print(plugboard.add("9"))
    print(plugboard.add("10"))
    
    print(plugboard.add("11"))
    

    result:

    ['SZ']
    ['SZ', 'AG']
    ['SZ', 'AG', 'CD']
    leads already have this item: CD
    ['SZ', 'AG', 'CD']
    ['SZ', 'AG', 'CD', '4']
    ['SZ', 'AG', 'CD', '4', '5']
    ['SZ', 'AG', 'CD', '4', '5', '6']
    ['SZ', 'AG', 'CD', '4', '5', '6', '7']
    ['SZ', 'AG', 'CD', '4', '5', '6', '7', '8']
    ['SZ', 'AG', 'CD', '4', '5', '6', '7', '8', '9']
    ['SZ', 'AG', 'CD', '4', '5', '6', '7', '8', '9', '10']
    leads already got 10 items
    ['SZ', 'AG', 'CD', '4', '5', '6', '7', '8', '9', '10']