编程语言
首页 > 编程语言> > python实例013--定义一个矩形类

python实例013--定义一个矩形类

作者:互联网

定义一个矩形类 可以获取周长和面积

import sys
class Rectangle:
    def __init__(self,x,y):
        self.width=y
        self.height=x
    
    def getLength(self):
        return (self.width+self.height)*2
    
    def getArea(self):
        return self.width*self.height
    
if __name__=="__main__":
    a,b=0.0,0.0
    try:
        a=float(input("请输出矩形的长:\n"))
        b=float(input("请输入矩形的宽:\n"))
    except ValueError:
        print("输入有误,请输入数字:")
        sys.exit()
    rectangle=Rectangle(a, b)
    
    print("长为{height}宽为{width}的矩形 周长为:{0:.3f} 面积为:{1:.3f}".format(rectangle.getLength(), rectangle.getArea(),height=rectangle.height,width=rectangle.width))

标签:__,width,python,self,height,--,013,矩形,rectangle
来源: https://blog.csdn.net/dododododoooo/article/details/117223448