我在项目中遇到了一些困难。我试图给一个列表项分配一个变量,调用该项,然后无限期地重复这个过程。我在龟里做这个。迭代列表,为每个项目分配一个变量并将其返回
该代码的目的是绘制一个彩色圆圈。目前,我已经设置它从列表中随机选择一种颜色。我宁愿它从头到尾遍历列表,并反复在列表中绘制下一个颜色。
import turtle as t
import random as r
# list of shades of blue
colourBlue = ['midnight blue', 'navy', 'cornflower blue', 'dark slate blue',
'slate blue', 'medium slate blue', 'light slate blue', 'medium blue', 'royal
blue', 'blue', 'dodger blue', 'deep sky blue']
# Call a colour from the list and draw a circle of said colour
def circle():
t.pendown()
t.begin_fill()
t.color(r.choice(colourBlue))
t.circle(10)
t.end_fill()
t.penup()
# Defines a function that loops through ColourBlue list
def colourPick():
colourBlueLen = len(colourBlue)
for i in range(11, colourBlueLen):
i = colourBlue[0]
到目前为止,我已经建立了一种方法来在列表中选择一个项目,但我不确定要我应该怎么把它分配给一个变量,在t.color()
函数中调用它和整个列表重复此过程。
您可以通过执行'currentColour =(currentColour + 1)%len(colourBlue)'来使用模运算符('%')而不是'if'语句,这会将'currentColour'保持在正确的范围内。 – cdlane