I am trying to do an excersise that involves Classes and Class inheritance In Python.
I keep having problems with trying to add the SpecialInfo input to the values for the fruits. The error that I’m getting is (“SpecialItem” object has no attribute “SpecialInfo”).
I would be very grateful if anyone could lend me a hand with this.
The code is shown here:
class Item():
def __init__(self, Description, Number, UnitPrice):
self.Description = Description
self.Number = Number
self.UnitPrice = UnitPrice
print("Create new item: ")
self.PrintItemInfo()
def PrintItemInfo(self):
print(self.Description, self.Number, "£", self.UnitPrice)
def __str__(self):
return self.Description
class SpecialItem(Item):
def __init__(self,Description, Number, UnitPrice, SpecialInfo):
super().__init__(Description,Number,UnitPrice)
self.SpecialInfo = SpecialInfo
print(SpecialInfo)
def __str__(self, Description,SpecialInfo):
return self.Description, self.SpecialInfo
def PrintItemInfo(self):
print(self.Description, self.Number,self.UnitPrice, self.SpecialInfo)
Apple = Item("Apple", 1, 0.5)
Apple.PrintItemInfo()
Banana = Item("Banana", 6, 3)
Carrot = Item("Carrot", 8, 2)
Oranges = SpecialItem("Orange", 3, 4, "they are orange")
ShoppingList = [Apple, Banana, Carrot, Oranges]
print("Total shopping list: ")
Sum = 0
for i in ShoppingList:
x = i.Number * i.UnitPrice
Sum += x
print(i.Description,i.Number, "£", x)
print("£", Sum)