我有以下代码来创建一个shape类,我有两个问题,我希望得到答案:
1运行以下代码时,输出为:>>>
100
100
None
>>>
结尾的“无”是什么?我如何才能消除此输出?
2。理想情况下,我希望能够(在输出屏幕)绘制一个正方形。我不想使用pygame。我确实想知道是否可以集成turtle来完成这项工作,但不知道如何开始?有什么建议可以使用turtle实现这一点,或者其他天才的建议?在
^{pr2}$
我可以补充说,在SO上也有类似的问题,但没有具体或有用的答案
更新:
我试过这样的方法,但没能奏效。我想我需要在构造器中的某个地方初始化turtle,但是在哪里以及如何初始化呢from turtle import*
class Shape:
#self is how we refer to things in the clas from within itself. .self is the first parameter in any function defined inside a class
#to access functions and variables inside the class, their name must be preceded with self and a full-stop (e.g. self.variable_name)
def __init__(self,x,y):
self.x=x #the shape has the attribute x (width)
self.y=y #the shape has the attribute y (height)
description="The shape has not yet been brought into being"
author="No one has yet claimed authorship of this shape"
def area(self):
return self.x*self.y
def perimeter(self):
return 2*self.x+2*self.y
def describe(self,text):
self.description =text
def authorName(self,text):
self.author=text
def scaleSize(self,scale):
self.x=self.x*scale
self.y=self.y*scale
def print(self,shapename):
print("This shape is a", shapename, "with dimensions:>",self.x,"by",self.y)
def draw(self):
turtle.forward(self.x)
turtle.left(90)
turtle.forward(se.f.x)
turtle.left(90)
turtle.forward(self.y)
turtle.left(90)
turtle.forward(self.y)
turtle.left(90)
square=Shape(100,100)
square.print("square")
print("The perimeter is:",square.perimeter())
print(square.draw())