在Python中使用变量的重复模式

1 投票
1 回答
1910 浏览
提问于 2025-04-28 06:54

我正在尝试用Python写一个可以重复图案的程序。这个程序会问你想要重复的形状有多少条边、要重复多少次,以及背景和形状填充的颜色。它应该在每次绘制形状后,按照边数的多少来转动360度除以边数的角度。然而,现在它总是停在原地不停地重复。我的代码如下。

from turtle import*

bgColor = input("What colour background do you want?:  ")
fillColor = input("What colour shape fill do you want?:  ")
numberOfSides = int(input("How many sides?:  "))
rotationsWanted = int(input("How many rotations?:  "))
rotationsCompleted = 0

def drawshape():
    fillcolor(fillColor)
    bgcolor(bgColor)
    begin_fill()
    while (rotationsCompleted < rotationsWanted):

        for x in range(numberOfSides):

            forward (100)
            right(360/numberOfSides)

        end_fill()

drawshape()
right(360/rotationsWanted)
rotationsCompleted = rotationsCompleted + 1
暂无标签

1 个回答

0

试着修改一下 while 循环

rotationsCompleted = 0
while (rotationsCompleted < rotationsWanted):
    rotationsCompleted = rotationsCompleted + 1

然后在 end_fill() 之后,你可以去另一个位置,可能使用 goto(100, 100) 来在不同的位置绘制下一个形状。

撰写回答