[英]How do I return my variable from a function in Python?
How do I return my getArea?如何返回我的 getArea? It is currently giving my None (default) as a result and I cannot fathom what piece is missing without the extra eyes.
结果,它目前给出了我的 None (默认),如果没有额外的眼睛,我无法理解缺少的部分。
Here I am focused on my Carpet Class which creates objects using the Rectangle class.在这里,我专注于我的地毯 Class,它使用矩形 class 创建对象。 Am I putting in the wrong print statement to test the getArea method of the Carpet class, or is the getArea method completely wrong?
我是否输入了错误的打印语句来测试地毯 class 的 getArea 方法,还是 getArea 方法完全错误? (Everything for Rug class worked as expected).
(地毯 class 的一切都按预期工作)。 Thank you in advance!
先感谢您!
class Rectangle:
# initialize
def __init__(self, length, width):
self.length = length
self.width = width
# length pair
def getLength(self):
return self.length
def setLength(self, length):
self.length = length
#width pair
def getWidth(self):
return self.width
def setWidth(self, width):
self.width = width
class Rug(Rectangle):
def __init__(self, length = 5, width = 7, price = 34.99):
super().__init__(length, width)
self.price = '$' + str(price)
def getPrice(self):
return self.price
class Carpet(Rectangle):
def __init__(self, length, width, price_per_square_foot = 1.99):
super().__init__(length, width)
self.price_per_square_foot = price_per_square_foot
def getArea(self):
self.area = self.length * self.width
def getPrice(self):
return self.area * self.price_per_square_foot
carp_1 = Carpet(3, 4)
print(carp_1.length)
print(carp_1.width)
print(carp_1.price_per_square_foot)
print(carp_1.getArea())
print(carp_1.getPrice())
'''rug_1 = Rug()
print(rug_1.length)
print(rug_1.width)
print(rug_1.price)'''
You have to return self.length * self.width afterwords您必须返回 self.length * self.width 后记
You need to add a return statement to the getArea
method.您需要在
getArea
方法中添加一个 return 语句。 It should look like this:它应该如下所示:
def getArea(self):
self.area = self.length * self.width
return self.area
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.