简体   繁体   English

如何从 Python 中的 function 返回我的变量?

[英]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.

相关问题 Python - 如何返回从导入的 Python 函数定义的变量? - Python - How do I return a variable defined from an imported python function? Python:如何避免在通过 function 传递全局变量时对其进行更新? - Python: How do I avoid my global variable from being updated when passing it through a function? 如何从 Python 中的异步函数访问返回值? - How do I access the return value from an async function in Python? 我如何从 Python 函数返回多个变量 - How do i return multiples variables from Python function 如何在Python 3中从函数中打印这些返回值? - How do I print these return values from function in Python 3? 使用 python 和flask-sqlalchemy,如何使用动态变量作为列名从我的表中返回一个值? - Using python and flask-sqlalchemy, how do I return a value from my table using a dynamic variable as the column name? 如何从正在运行的python函数获取变量 - How do I get a variable from a running python function 如何从返回2个值的函数中为返回值分配2个不同的变量? - How do I assign 2 different variable to the return values from a function that return 2 values? 如何将我的列表变量传递回我的函数之外(Python) - How do I pass my list variable back outside of my function (Python) Python:如何分配从1个输入返回的2个值作为函数外部的值? - Python: How do I assign 2 values I return from a function with 1 input as values outside the function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM