# 设计一个名为 MyRectangle 的矩形类来表示矩形。
'''这个类包含
(1) 左上角顶点的坐标:x,y
(2) 宽度和高度:width、height
(3) 构造方法:传入 x,y,width,height。如果(x,y)不传则默认是 0,如果 width和 height 不传,则默认是 100.
(4) 定义一个 getArea() 计算面积的方法
(5) 定义一个 getPerimeter(),计算周长的方法
(6) 定义一个 draw()方法,使用海龟绘图绘制出这个'''
import turtle
class MyRectangle:
def __init__(self, x, y, width, height):
self.x = x
self.y = y
self.width = width
self.height = height
if x == "" or y == "":
x == 0 and y == 0
elif width == "" or height == "":
width == 100 and height == 100
return "左上角顶点的坐标:{0},{1},矩形的宽:{2},矩形的高:{3}".format(self.x, self.y, self.width, self.height)
def getArea(self):
s = self.width * self.height
print("面积是:", s)
def getPerimeter(self):
c = (self.width + self.height) * 2
print("周长是:", c)
def draw(self):
turtle.penup()
turtle.goto(self.x, self.y) # 开始起点
turtle.pendown() # 下笔
turtle.goto(self.x, self.y - self.height)
turtle.goto(self.x + self.width, self.y - (self.height))
turtle.goto(self.x + self.width, self.y)
turtle.goto(self.x, self.y)
turtle.done() # 保持绘画窗口不消失
s = MyRectangle(x="", y="", width="", height="")
s.getArea()
s.getPerimeter()
s.draw()
老师:这断代码老是报错,找了半天也不知道错在哪里?
Traceback (most recent call last):
File "C:/Users/Administrator/PycharmProjects/mypro_exception/test.py", line 43, in <module>
s = MyRectangle(x="", y="", width="", height="")
TypeError: __init__() should return None, not 'str'
Process finished with exit code 1