我在項目中遇到了一些困難。我試圖給一個列表項分配一個變量,調用該項,然後無限期地重複這個過程。我在龜裏做這個。迭代列表,爲每個項目分配一個變量並將其返回
該代碼的目的是繪製一個彩色圓圈。目前,我已經設置它從列表中隨機選擇一種顏色。我寧願它從頭到尾遍歷列表,並反覆在列表中繪製下一個顏色。
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