This is a code for my school pls help
class product:
deliveryCharge=50
def __init__(self,nam="Teddy Bear", prc=500):
self.name=nam
self.price=prc
def get_name(self):
return self.name
def get_price(self):
return self.price product.deliveryCharge
def __str__(self):
return "The {} will cost you Rs.{}.".format(self.get_name(),self.get_price())
class gift(product):
def __init__(self,nam,prc,wrpchrge=100):
super().__init__(nam,prc)
self.wrappingcharge=wrpchrge
def get_price(self):
return self.price product.deliveryCharge gift.wrappingcharge
x1=product("yoyo",29)
print("I am buying a {} and it costs me {}".format(x1.get_name,x1.get_price))
m1=gift("yoyo",29)
print("I am buying a {} and it costs me {}".format(m1.get_name,m1.get_price))
print(product.get_name)
The error I am getting:
If anyone knows how to fix this,
CodePudding user response:
You are printing the reference to the objects in memory. Use parenthesis to print the actual values. For instance x1.get_name
should be x1.get_name()
. There are also other problems, you need to use self to reference the values in both classes, even in the gift class for values in product, since it inherits its functions.
Here's an example:
class product:
deliveryCharge=50
def __init__(self,nam="Teddy Bear", prc=500):
self.name=nam
self.price=prc
def get_name(self):
return self.name
def get_price(self):
return self.price self.deliveryCharge # changed from product to self
def __str__(self):
return "The {} will cost you Rs.{}.".format(self.get_name(),self.get_price())
class gift(product):
def __init__(self,nam,prc,wrpchrge=100):
super().__init__(nam,prc)
self.wrappingcharge=wrpchrge
def get_price(self):
return self.price self.deliveryCharge self.wrappingcharge # changed to self
x1=product("yoyo",29)
print("I am buying a {} and it costs me {}".format(x1.get_name(),x1.get_price()))
m1=gift("yoyo",29)
print("I am buying a {} and it costs me {}".format(m1.get_name(),m1.get_price()))
print(x1.get_name()) # changed to x1 from product
Result:
I am buying a yoyo and it costs me 79
I am buying a yoyo and it costs me 179
yoyo