【问题标题】:Drawing a square on python turtle在蟒蛇龟上画一个正方形
【发布时间】:2020-10-29 17:50:12
【问题描述】:

enter image description here我是一名年轻的法国学生。 我需要你的帮助。我有一个愚蠢的 python 练习,我无法解决,因为我是初学者。我正在研究 python 3.7。我的老师让我提出一个程序,从坐标 0.0 开始,在每边绘制一个 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()

如果你能帮助我编写一个非常基本的代码。

【问题讨论】:

  • 嗨,Thomas,欢迎来到堆栈溢出。通常,了解您尝试过的内容以及遇到的具体错误对我们很有帮助。 Stack Overflow 不应该是“做我的功课”之类的事情。
  • 嘿,Thomas,我很惊讶你的老师让你这么直截了当,你一定上过一些关于迭代等的课程。你失败的尝试是什么样的?分享你的代码?!
  • 我不知道对不起,我只是需要帮助。 @griffin_cosgrove

标签: python draw square


【解决方案1】:

代码应如下所示:

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 命令让它跑得更快。另外,网上有很多教程,所以我认为这些会更适合这种情况。

【讨论】:

  • 当我在 python 3.7 中尝试时,他们告诉我 SyntaxError: multiple statements found while compile a single statement
  • 抱歉,python 3.7 不是 3.8
【解决方案2】:
#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

【讨论】:

    猜你喜欢
    • 2021-04-20
    • 1970-01-01
    • 1970-01-01
    • 2018-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-01
    • 1970-01-01
    相关资源
    最近更新 更多