绘制正方形python_在Python中用实际绘制的正方形实现形状类?

我有以下代码来创建一个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())

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值