我有下面的Python程序来绘制一个正方形的设计使用下面列出的颜色。程序只对所有框应用粉红色,如何使语法按下面列出的顺序重复颜色?你知道吗
import turtle
def main():
t = turtle.Turtle()
t.hideturtle()
t.speed(500)
color = ["pink", "navy blue","red","forest green","cyan","magenta"]
squaredesign(t,color)
def squaredesign(t,color):
x = 100
y = 100
z = 1
c = 0
for i in range(10):
t.up()
t.goto(x,y)
t.down()
t.goto(x-x-x,y)
t.goto(x-x-x,y-y-y)
t.goto(x,y-y-y)
t.goto(x,y)
x+=-10
y+=-10
t.pencolor(color[c])
main()
我喜欢使用来自
itertools
的cycle
函数:它从来没有用完的颜色,因为它只是从一开始就在它到达结束后重新开始。模数运算符(%)是解决此问题的另一种方法:
从而避免了额外的导入。你知道吗
相关问题 更多 >
编程相关推荐