2017-09-24 68 views
-1

我在项目中遇到了一些困难。我试图给一个列表项分配一个变量,调用该项,然后无限期地重复这个过程。我在龟里做这个。迭代列表,为每个项目分配一个变量并将其返回

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

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

回答

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

您可以通过执行'currentColour =(currentColour + 1)%len(colourBlue)'来使用模运算符('%')而不是'if'语句,这会将'currentColour'保持在正确的范围内。 – cdlane

0

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

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

,然后用它代替r.choice(colourBlue)该参数。

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

enter image description here

如果你宁愿只一次穿过颜色列表,这也很简单。只需使用颜色列表本身作为你的目标迭代:

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

enter image description here

+0

太棒了,非常感谢。我试着用Google搜索解决方案,从来没有遇到过itertools或循环函数。肯定会利用它的未来。 – svantem

相关问题