我是 python 和海龟的新手,希望得到一些帮助。 我正在尝试创建一个程序,该程序接受多条边的输入,然后绘制具有该边数的正多边形。然而,它要么产生 TimeLimitError,要么只是画一条直线。
这是我所拥有的:
sides = int(input("How many sides would you like? "))
angle = sides / 360
import turtle
for count in range(sides):
turtle.fd(50)
turtle.lt(angle)
但这就是它不断生产的:
How many sides would you like? 5
TimeLimitError: Program exceeded run time limit. on line 1
您应该将 360 除以边数,而不是相反。
angle = 360 / sides
谢谢您的代码。我稍微改变了它。它没有被用作测试的答案。 (这是在 Grok:Blockly Playground 上完成的)我将在下面发布:
from turtle import *
print('I claim no credit to this code.')
print('This was created by lenawb on stackoverflow')
print('https://stackoverflow.com/questions/46714190/using-python-
turtle-to-draw-a-polygon-with-n-number-of-sides')
Fill_colour = input('Fill colour?' + ' ')
left_or_right_ = input('Left or right?' + ' ')
Side_Length = int(input('Length?' + ' '))
Sides = int(input('Sides?' + ' '))
angle = 360 / Sides
PEN_Size = int(input('Pen size?' + ' '))
pensize(PEN_Size)
fillcolor(Fill_colour)
begin_fill()
if left_or_right_ == 'left':
for count in range(int(Sides)):
forward(Side_Length)
left(angle)
else:
for count2 in range(int(Sides)):
forward(Side_Length)
right(angle)
end_fill()