0

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.price

AttributeError: Sweet instance has no attribute 'price'

NOTE: The error is thrown when the user wants to know the price of the chosen fruit.

James Mertz
8,81912 gold badges63 silver badges88 bronze badges
asked Aug 7, 2014 at 20:09
5
  • 1
    Please cut this down to a minimal example; we don't need to see everything. But you never assign self.price, so this shouldn't have been surprising. Commented Aug 7, 2014 at 20:12
  • And where do you think you are setting the price? Point to the code that does it. Commented Aug 7, 2014 at 20:14
  • For the love of all that is pythonic, please wrap up your script code with a if __name__ == "__main__":. Commented Aug 7, 2014 at 20:28
  • Also, why bother always passing 'salty' to Salt? Surely taste should be a Fruit attribute, then e.g. Salt doesn't take that argument and always passes 'salty' to super(Salt, self).__init__(...)? That way all Fruit has a taste, and all Salt has taste == 'salty'. Commented Aug 7, 2014 at 20:47
  • How can I do the wrapping of this code using if ______name______ = "______main______" could you please paste the code here.Thanks. Commented Aug 8, 2014 at 10:23

3 Answers 3

4

You need self.price=price in the __init__ methods - currently you are just throwing that parameter away.

answered Aug 7, 2014 at 20:14
Sign up to request clarification or add additional context in comments.

Comments

2

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
 (..)
answered Aug 7, 2014 at 20:14

1 Comment

Strictly not to the classes, but to the instances.
1

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

answered Aug 7, 2014 at 20:22

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.