-2

I created this small program. It should add to the cupboard some products. I get the following error:

TypeError: add_prod() missing 1 required positional argument: 'product'. 

Here is the code:

class Products:

    def __init__(self,nome):
        self.nome = nome

class Cupboard:

    def __init__(self):
        self.prod = []
    def add_prod(self, product):
        self.prod.append(product)

    def show(self):
        print(self.prod)


p1 = Products("Ajax")
p2 = Products("Barilla")

Cupboard.add_prod(p1)
Cupboard.add_prod(p2)

show()
Matteo Possamai
  • 455
  • 1
  • 10
  • 23

1 Answers1

1

In your code when you are trying to add Products to Cupboard, you are trying to do it to the class itself instead of to an instance. You should create an instance of Cupboard first like you did for Products:

p1 = Products("Ajax")
p2 = Products("Barilla")

c1 = Cupboard()
c1.add_prod(p1)
c1.add_prod(p2)
Mark Loeser
  • 17,657
  • 2
  • 26
  • 34