aimcreeper aimcreeper - 3 months ago 15
Python Question

__init__() missing 1 required positional argument: 'quantity'

I am getting the error as shown in the question, and I can't figure out why. Even when trying other stackoverflow methods of fixing this it doesn't work.

Thanks all :)

class Item(object):
def __init__(self, name, style, quantity):
self.name = name
self.style = style
self.quantity = quantity

def itemadd(self):
inventory.append(Item)

class Weapon(Item):
def __init__(self, name, style, quantity = 1):
Item.__init__(name, style, quantity,)

def weaponadd(self):
inventory.append(Weapon)


class Ammo(Item):
def __init__(self, name, style, quantity = 1):
Item.__init__(name, style, quantity)

def ammoadd(self):
inventory.append(Ammo)


class Armour(Item):
def __init__(self, name, style, quantity = 1):
Item.__init__(name, style, quantity)

def armouradd(self):
inventory.append(Armour)

Bow = Weapon(name = "Bow", style = "WRanged", quantity = 1)


Edit: Thanks everyone, this question has been answered :)

Edit 2: The lines affected with the error codes:

Traceback (most recent call last):
File "C:\Stuff\SG\Work\Inventory.py", line 33, in <module>
Bow = Weapon(name = "Bow", style = "WRanged", quantity = 1)
File "C:\Stuff\SG\Work\Inventory.py", line 12, in __init__
Item.__init__(name, style, quantity,)
TypeError: __init__() missing 1 required positional argument: 'quantity'


Sidenote: All answers below are correct

Answer

Change

Item.__init__(name, style, quantity,)

for

super().__init__(name, style, quantity)