在python乌龟上画一个正方形

问题描述

enter image description here我是一位年轻的法国学生。 我需要你的帮助。我有一个愚蠢的python练习,因为我是初学者而无法解决。我正在使用python 3.7。我的老师要求我提出一个程序,从坐标0.0(使用turtle模块)开始,在每侧绘制150的正方形,并用蓝色填充。我已经尝试了好几次,但是我无法处理。如果您知道我确实很少有知识,可以帮助我解决问题。我可以使用的命令是: 重新

set()
goto(x,y)
forward(distance)
backward(distance)
circle(radius)
up()
down()
colour(colour)
left(angle)
right(angle)
width(thickness)
begin_fill()
end_fill()
write(text)
done() or mainloop()

如果您可以通过一个非常基本的代码来帮助我。

解决方法

代码应如下所示:

import turtle
t=turtle.Turtle()
x=0
t.fillcolor("blue")
t.begin_fill()
while x<4:
    t.forward(150)
    t.left(90)
    x+=1
t.end_fill()
turtle.done()

turtle.done()充当mainloop()命令。 这将创建一个蓝色的正方形。如果您希望它运行得更快,请使用.speed命令使其运行得更快。另外,在线上有很多教程,所以我认为这种情况会更好。

,
#import library
from turtle import *

# set color
fillcolor("blue") 
  
# start filling area 
begin_fill() 
      
# draw the square 
for _ in range(4): 
   forward(150) 
   right(90) 
      
# ending the filling 
end_fill() 

记住要安装python3-tk