设计一个矩形类,实现传入x、y、width、height 打印输入的值、面积、周长、海龟绘图

import turtle
class MyRectangle:
    def __init__(self,x=0,y=0,width=100,height=100):
        #赋值
        self.x = x
        self.y = y
        self.width = width
        self.height = height

    def say_score(self): #输出各值
        print('输入的数据分别是:{0} {1} {2} {3}'.format(self.x, self.y, self.width, self.height))

    def get_Area(self):
        s = self.width * self.height
        print("矩形面积为:{0}".format(s))

    def get_Perimeter(self):
        c = (self.width+self.height)*2
        print('矩形的周长是{0}'.format(c))
    def draw(self):
        turtle.penup()
        turtle.goto(self.x,self.y)
        turtle.pendown()
        turtle.fillcolor("yellow")
        turtle.begin_fill()
        for x in range(1,5):
            if x%2 == 1:
                d = self.width
            else:
                d = self.height
            turtle.forward(d)
            turtle.right(90)
        turtle.end_fill()
        turtle.penup()
        turtle.done()
def l():
    a,b,w,h = None,None,None,None
    #判断是否输入数值及获取数值
    for x in range(4):
        x = int(input('输入1、2、3、4分别代表输入x,y,宽,高的值,输入0退出循环'))
        if x == 1:
            a = int(input("输入x坐标的值"))
        elif x == 2:
            b = int(input("输入y坐标的值"))
        elif x == 3:
            w = int(input("输入宽坐标的值"))
        elif x == 4:
            h = int(input("输入高坐标的值"))
        else:
            break

    #判断输入了哪几个值且执行类函数
    if a is not None and b is None and w is None and h is None:
        print(a)
        s1=MyRectangle(x=a)
        s1.say_score()
        s1.get_Area()
        s1.get_Perimeter()
        s1.draw()
    elif b is not None and a is None and w is None and h is None:
        s1 = MyRectangle(y=b)
        s1.say_score()
        s1.get_Area()
        s1.get_Perimeter()
        s1.draw()

    elif w is not None and a is None and b is None and h is None:
        s1 = MyRectangle(width=w)
        s1.say_score()
        s1.get_Area()
        s1.get_Perimeter()
        s1.draw()
    elif h is not None and a is None and b is None and w is None:
        s1 = MyRectangle(height=h)
        s1.say_score()
        s1.get_Area()
        s1.get_Perimeter()
        s1.draw()
    elif a is not None and b is not None and w is None and h is None:
        s1 = MyRectangle(x=a,y=b)
        s1.say_score()
        s1.get_Area()
        s1.get_Perimeter()
        s1.draw()
    elif a is not None and b is not None and w is not None and h is None:
        s1 = MyRectangle(x=a,y=b,width=w)
        s1.say_score()
        s1.get_Area()
        s1.get_Perimeter()
        s1.draw()
    elif a is not None and b is not None and w is not None and h is not None:
        s1 = MyRectangle(x=a,y=b,width=w,height=h)
        s1.say_score()
        s1.get_Area()
        s1.get_Perimeter()
        s1.draw()
    elif a is not None and w is not None and h is not None and b is None:
        s1 = MyRectangle(x=a, width=w, height=h)
        s1.say_score()
        s1.get_Area()
        s1.get_Perimeter()
        s1.draw()
    elif a is not None and w is not None and b is None and h is None:
        s1 = MyRectangle(x=a, width=w)
        s1.say_score()
        s1.get_Area()
        s1.get_Perimeter()
        s1.draw()
    elif b is not None and w is not None and a is None and h is None:
        s1 = MyRectangle(y=b,width=w)
        s1.say_score()
        s1.get_Area()
        s1.get_Perimeter()
        s1.draw()
    elif b is not None and w is not None and h is not None and a is None:
        s1 = MyRectangle(y=b,width=w,height=h)
        s1.say_score()
        s1.get_Area()
        s1.get_Perimeter()
        s1.draw()
    elif b is not None and h is not None and a is None and w is None:
        s1 = MyRectangle(y=b,height=h)
        s1.say_score()
        s1.get_Area()
        s1.get_Perimeter()
        s1.draw()
    elif w is not None and h is not None and a is None and b is None:
        s1 = MyRectangle(width=w,height=h)
        s1.say_score()
        s1.get_Area()
        s1.get_Perimeter()
        s1.draw()
    elif a is None and b is None and w is None and h is None:
        print('你什么都没输入,将以默认值设定')
        s1 = MyRectangle()
        s1.say_score()
        s1.get_Area()
        s1.get_Perimeter()
        s1.draw()

l()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值