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.
3 Answers 3
You need self.price=price in the __init__ methods - currently you are just throwing that parameter away.
Comments
You need to add self.price, preferably to Fruit:
class Fruit:
def __init__(self,name,price):
self.name = name
self.price = price #set price when initiated.
(...)
class Sweet(Fruit):
def __init__(self,name,taste,price):
Fruit.__init__(self,name,price) #send price as init parameter
self.taste = taste
print "Initialized the name of the fruit as %s" % self.name
(..)
1 Comment
Maybe is a better option add that attribute to you parent class fruit
class Fruit:
def __init__(self,name, price):
self.name = name
self.price = price
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, price)
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, price)
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
Works fine as well. Also you can add a getter in your Fruit class to obtain the price, saving lines of code and using the advantage given by the OOP because your children classes are inheriting the method
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'.