我对我的一个项目有些困难。我试图将一个变量分配给一个列表项,调用该项,然后无限期地重复该过程。我在Turtle中做这件事。
代码的目的是绘制一个彩色圆圈。目前,我设置它从列表中随机选择一种颜色。我宁愿它从头到尾遍历列表并重复绘制列表中的下一个颜色。
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()
函数中调用它并在整个过程中重复该过程清单。
答案 0 :(得分:1)
我宁愿它从头到尾反复浏览列表 绘制下一个颜色
如果您想按顺序处理颜色列表,但又不想受列表本身约束,我建议itertools.cycle()
。它允许您在不考虑实际颜色数量的情况下,一次又一次地遍历颜色列表,而不考虑实际颜色数量:
from itertools import cycle
from turtle import Turtle, Screen
# list of shades of blue
BLUE_SHADES = cycle(['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(turtle):
turtle.color(next(BLUE_SHADES))
turtle.begin_fill()
turtle.circle(50)
turtle.end_fill()
screen = Screen()
yertle = Turtle(visible=False)
yertle.speed('fastest')
for _ in range(120):
circle(yertle)
yertle.right(3)
screen.exitonclick()
如果您只想通过颜色列表工作一次,那也很容易。只需使用颜色列表本身作为迭代目标:
from turtle import Turtle, Screen
# list of shades of blue
BLUE_SHADES = ['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(turtle, color):
turtle.color(color)
turtle.begin_fill()
turtle.circle(50)
turtle.end_fill()
screen = Screen()
yertle = Turtle(visible=False)
yertle.speed('fastest')
for shade in BLUE_SHADES:
circle(yertle, shade)
yertle.right(360 / len(BLUE_SHADES))
screen.exitonclick()
答案 1 :(得分:0)
我认为您只想将参数传递给circle
:
def colourPick():
for c in colourBlue:
circle(c)
然后使用该参数代替r.choice(colourBlue)
。
答案 2 :(得分:-1)
我设法在朋友的帮助下找到解决方案。
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']
currentColour = 0
# Establishes a function that calls a each colour from the list
def circle():
t.pendown()
t.begin_fill()
t.color(colourPick())
t.circle(10)
t.end_fill()
t.penup()
def colourPick():
global currentColour
colourBlueLen = len(colourBlue)
# If the last colour in the list has been used, reset it back to 0
if currentColour == colourBlueLen:
currentColour = 0
colour = colourBlue[currentColour]
# Increment currentColour values
currentColour += 1
return colour
circle()