似乎无法从我的类中获取输出

2024-04-29 01:11:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我似乎不明白我的代码出了什么问题,以及为什么我什么都没有得到。我似乎也不知道如何展开矩形。 说明如下: 创建一个名为expand的方法,该方法接受一个偏移量值,并返回一个在所有方向上都以偏移量展开的矩形的副本。在

>>> r = Rectangle(30, 40, 100, 110)
>>> print(r)    
   Rectangle(30, 40, 100, 110)

>>> r1 = r.expand(offset=3) 
>>> print(r1)
   Rectangle(27, 37, 106, 116)    

不应修改原始矩形。在

^{pr2}$

负值应返回缩小的矩形。在

^{3}$

到目前为止,我得到的是:

class Rectangle:

    def __init__(self, initx, inity, initw, inith):

        self.x = initx

        self.y = inity

        self.w = initw

        self.h = inith

    def __str__(self):
        return('Rectangle(' + str(self.x) + ',' + str(self.y) + ','
                            + str(self.w) + ',' + str(self.h)+')')
    def right(self):
        return self.x + self.w


    def top(self):
        return self.y + self.h


    def size(self):
        return '(' + self.w + ',' + self.h+ ')'

    def position(self):
        return '(' + self.x + ',' + self.y + ')'

    def area(self):
       return self.w * self.h

    def expand(self):
    # can't figure this part out


    r = Rectangle(5,10,50,100)
    r2 = Rectangle(5,10,50,100)
    r3 = Rectangle(3,5,10,20)
    r4 = Rectangle(12,10,72,35)
    r5 = Rectangle(5,7,10,6)
    r6 = Rectangle(1,2,3,4)


    print(r2)

    print(r3.right())

    print(r4.right())

    print(r5.top())

    print(r6.size())

    print(r6.position())

    print(r6.area())

Tags: 方法selfrightreturndef偏移量expandprint
2条回答

您需要通过使用偏移添加的矩形点从展开创建新对象。在

def expand(self, offset):
    # can't figure this part out
    return type(self)(self.x - offset, self.y - offset
                self.w + offset, self.h + offset)
def expand(self,expand):
    return Rectangle(self.x - expand, self.y - expand, self.w + 2*expand, 
                      self.h + 2*expand)

或者更通用的方法来防止未来的过载。。。。在

^{pr2}$

还有。。。。负维度呢?也许最好在构造函数中修复它:

def __init__(self, initx, inity, initw, inith):
    self.x = initx
    self.y = inity
    self.w = max(0,initw)
    self.h = max(0,inith)

最后,我们可以讨论问题的主题:似乎无法从我的类获得输出。也许是缩进的问题,我会用我的方法重写你的脚本,并修复缩进问题。在

class Rectangle:

    def __init__(self, initx, inity, initw, inith):
        self.x = initx
        self.y = inity
        self.w = max(0,initw)
        self.h = max(0,inith)

    def __str__(self):
        return('Rectangle(' + str(self.x) + ',' + str(self.y) + ','
                            + str(self.w) + ',' + str(self.h)+')')
    def right(self):
        return self.x + self.w

    def top(self):
        return self.y + self.h

    def size(self):
        return '(' + self.w + ',' + self.h+ ')'

    def position(self):
        return '(' + self.x + ',' + self.y + ')'

    def area(self):
       return self.w * self.h

    def expand(self,expand):
       return type(self)(self.x - expand, self.y - expand, self.w + 2*expand, 
                      self.h + 2*expand)


r = Rectangle(5,10,50,100)
r2 = Rectangle(5,10,50,100)
r3 = Rectangle(3,5,10,20)
r4 = Rectangle(12,10,72,35)
r5 = Rectangle(5,7,10,6)
r6 = Rectangle(1,2,3,4)

print(r2)
print(r3.right())
print(r4.right())
print(r5.top())
print(r6.size())
print(r6.position())
print(r6.area())

现在它应该像你期望的那样工作。在代码中,您在类定义中放置了变量定义和print语句。在

相关问题 更多 >