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

Python 迭代一个列表,为每个项目分配一个变量并返回它,python,loops,range,turtle-graphics,Python,Loops,Range,Turtle Graphics,我的一个项目有点困难。我试图为列表项分配一个变量,调用该项,然后无限期地重复该过程。我在海龟体内做这件事 代码的目的是绘制一个彩色圆圈。目前,我已经设置好,它可以从列表中随机选择一种颜色。我宁愿它从头到尾地浏览列表,并在列表中反复绘制下一种颜色 import turtle as t import random as r # list of shades of blue colourBlue = ['midnight blue', 'navy', 'cornflower blue', 'dark

我的一个项目有点困难。我试图为列表项分配一个变量,调用该项,然后无限期地重复该过程。我在海龟体内做这件事

代码的目的是绘制一个彩色圆圈。目前,我已经设置好,它可以从列表中随机选择一种颜色。我宁愿它从头到尾地浏览列表,并在列表中反复绘制下一种颜色

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()
函数中调用它,并在整个列表中重复该过程。

我想您只想将一个参数传递给
circle

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]
def colourPick():
    for c in colourBlue:
        circle(c)
然后使用该参数而不是
r.choice(colorblue)

我宁愿它从头到尾反复地浏览清单 画下一种颜色

如果您想按顺序处理颜色列表,但又不想受列表本身的约束,我建议您使用
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()

太棒了,非常感谢。我试着用谷歌搜索解决方案,但从未遇到itertools或cycle函数。您可以使用模运算符(
%
)而不是
if
语句,方法是执行
currentColour=(currentColour+1)%len(colourBlue)
,使
currentColour
保持在正确的范围内。