2017-09-24 47 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

相關問題