迭代一个列表,为每个项目分配一个变量并 return 它

Iterate a list, assign a variable to each item and return it

我的一个项目遇到了一些困难。我正在尝试将变量分配给列表项,调用该项目,然后无限期地重复该过程。我正在 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]

到目前为止,我已经建立了一种方法来 select 列表中的一个项目,但我不确定我应该如何将它分配给一个变量,在 t.color() 函数中调用它以及在整个列表中重复该过程。

我想你只是想传递一个参数给 circle:

def colourPick():
    for c in colourBlue:
        circle(c)

然后使用该参数代替 r.choice(colourBlue)

I would rather it go through the list from start to end and repeatedly draw the next colour

如果您想按顺序浏览颜色列表,但又不想被列表本身束缚,我推荐 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()

在朋友的帮助下,我设法找到了解决方案。

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()