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()
设计一个矩形类,实现传入x、y、width、height 打印输入的值、面积、周长、海龟绘图
最新推荐文章于 2022-05-17 07:15:00 发布