I have written the below script using python:
class Fruit:
def __init__(self,name):
self.name = name
print "Initialized Fruit Name: %s" % self.name
def tell(self):
print "U have entered the fruit as %s" % self.name
class Sweet(Fruit):
def __init__(self,name,taste,price):
Fruit.__init__(self,name)
self.taste = taste
print "Initialized the name of the fruit as %s" % self.name
def tell(self):
Fruit.tell(self)
print "Taste of the fruit is %s" % self.taste
def sayPrice(self):
Fruit.tell(self)
print "The price of the fruit Mango is %d" % self.price
class Salt(Fruit):
def __init__(self,name,taste,price):
Fruit.__init__(self,name)
self.taste = taste
print "Initialized the name of the fruit as %s" % self.name
def tell(self):
Fruit.tell(self)
print "Taste of the fruit is %s" % self.taste
def sayPrice(self):
Fruit.tell(self)
print "The price of the fruit Strawberry is %d" % self.price
m = Sweet('Mango','sweet',100)
s = Salt('Strawberry','salty',50)
choice = raw_input("enter ur choice:(Mango/Strawberry):")
if choice == 'Mango':
m.tell()
else:
s.tell()
decision = raw_input("Do U want to know the price?(Y/N)")
if decision == 'Y' and choice == 'Mango':
m.sayPrice()
elif decision == 'Y' and choice == 'Strawberry':
s.sayPrice()
else:
print "Sad to see U go :(, please do visit next time again"
And below is the error I am getting:
Traceback (most recent call last):
File "C:/Python26/Inheritance_practice2.py", line 47, in
m.sayPrice()File "C:/Python26/Inheritance_practice2.py", line 21, in sayPrice
print "The price of the fruit Mango is %d" % self.priceAttributeError: Sweet instance has no attribute 'price'
NOTE: The error is thrown when the user wants to know the price of the chosen fruit.
self.price, so this shouldn't have been surprising.if __name__ == "__main__":.'salty'toSalt? Surelytasteshould be aFruitattribute, then e.g.Saltdoesn't take that argument and always passes'salty'tosuper(Salt, self).__init__(...)? That way allFruithas ataste, and allSalthastaste == 'salty'.