需要知道如何用python编写的人的帮助。任何人?

时间:2014-11-07 03:44:15

标签: python

由于紧急情况我错过了一次演讲,需要帮助才能完成任务。我只能靠自己完成并需要帮助完成,有人告诉我我做错了什么。下面是我编写的代码到目前为止,代码下面是运行时需要的结果。请帮忙!

class Rectangle: 
    def __init__ (self, H=0, W=0): 
        self.width = W 
        self.height = H 

@property 
    def area (self): 
        return self.width * self.height 

def main(): 
        print ("Rectangle a:") 
        a = Rectangle (5, 7) 
        #print ("area: {}".format(a.area)) 
        #print ("perimeter: {}".format(a.perimeter)) 
        print ("") 
        print ("Rectangle b:") 
        b = Rectangle() 
        b.width = 10 
        b.height = 20 
        #print (b.area) 
        #print (b.getStats()) 

main () 

正确创建Rectangle类后,输出应如下所示:

Rectangle a: 
area: 35 
perimeter: 24 
Rectangle b: 
width: 10 
height: 20 
area: 200 
perimeter: 60

2 个答案:

答案 0 :(得分:0)

我不确定你在做什么,但这是一个正常运作的矩形类:

class Rectangle: 
    def __init__ (self, n, h, w): 
        self.Name = n
        self.Assign(h, w)

    def Assign(self, h, w):
        self.width = w
        self.height = h 

    def Area (self): 
        return self.width * self.height 

    def Perimeter(self):
        return 2 * (self.width + self.height)


    def Display(): 
        print("Rectange " + self.Name ": ")
        print("Height: " + self.height)
        print("Width: " + self.width)
        print(self.Area())
        print(self.Perimeter())

这是一个正常运作的矩形类。

答案 1 :(得分:0)

def main():
print ("Rectangle a:")
a = Rectangle(5, 7)
print ("area:      {}".format(a.area))
print ("perimeter: {}".format(a.perimeter))

print ("")
print ("Rectangle b:")
b = Rectangle()
b.width = 10
b.height = 20
print (b.getStats())