[SOLVED] No output in classes | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 7

[SOLVED] No output in classes

Hi, The code isn't showing any output. Can someone tell me the reason- #!/usr/bin/python # python program for items and its two child classes perishable and non perishable class Items(object): def __init__(self,price): self.price = price def CalculateSalePrice(self): pass class Perishable(Items): def __init__(self,price): self.price = price def CalculateSalePrice(price): print('The price is:',(price+500)) class NonPerishable(Items): def __init__(self,price): self.price = price def CalculateSalePrice(price): print('The price is:',(price+(price * 0.1))) i = Items(10) i.CalculateSalePrice() i.CalculateSalePrice() Thanks

10th Jun 2018, 4:42 PM
Sachin Artani
Sachin Artani - avatar
9 Answers
+ 4
class Perishable(Items): def __init__(self,price): self.price = price def CalculateSalePrice(self): print('The price is:',(self.price+500)) class NonPerishable(Items): def __init__(self,price): self.price = price def CalculateSalePrice(): print('The price is:',(self.price+(self.price * 0.1)))
10th Jun 2018, 4:54 PM
Gopal Gautam
Gopal Gautam - avatar
+ 3
Gopal Gautam , not working 😐 「HAPPY TO HELP」 don't know how to do it.
10th Jun 2018, 7:04 PM
Sachin Artani
Sachin Artani - avatar
+ 3
Sachin you're creating object of base class "items" which have "CalculateSalePrice" method that does nothing (pass). You need to either print something from this base class's "CalculateSalePrice" method or need to create object of either "Perishable" or "NonPerishable" i = Perishable(10) #Or NonPerishable(10) i.CalculateSalePrice()
11th Jun 2018, 12:20 AM
Gopal Gautam
Gopal Gautam - avatar
10th Jun 2018, 4:53 PM
Sachin Artani
Sachin Artani - avatar
+ 2
Sachin Artani The reason its simple: Your call the CalculateSalePrice on an object that define it like with empy body (Item class)... Futhermore you dont use well inheritance... In your context you havnt to define the constructors in derived clasees (Perishabe and NonPerishable) or call super in they (that set price attr) like: class Perishable(Items): def __init__(price): super().__init__(price)
11th Jun 2018, 4:21 PM
KrOW
KrOW - avatar
+ 2
Sachin Artani You are welcome 👍👍👍
11th Jun 2018, 5:10 PM
KrOW
KrOW - avatar
+ 2
#No output in classes #Hi, #The code isn't showing any output. Can someone tell me the reason- #!/usr/bin/python # python program for items and its two child classes perishable and non perishable class Items(object): def __init__(self,price): self.price = price def CalculateSalePrice(self): #pass #Just a test p = Perishable(self) p.CalculateSalePrice() nP = NonPerishable(self) nP.CalculateSalePrice() class Perishable(Items): def __init__(self,item): self.price = item.price def CalculateSalePrice(self): print('The price is:',(self.price+500)) class NonPerishable(Items): def __init__(self,item): self.price = item.price def CalculateSalePrice(self): print('The price is:',(self.price * (self.price * 0.2))) i = Items(10.0) i.CalculateSalePrice() #I have fixed it so. I am not sure if you wanted like this.
12th Jun 2018, 2:34 AM
Ferhat Sevim
Ferhat Sevim - avatar
+ 1
Thank you guys 😊
11th Jun 2018, 8:08 AM
Sachin Artani
Sachin Artani - avatar
+ 1
Thanks KrOW for the kind help.
11th Jun 2018, 4:31 PM
Sachin Artani
Sachin Artani - avatar