2017-09-24 9 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'ステートメントの代わりにモジュラス演算子('% ')を使うことができます。 – 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

本当にありがとうございます。私は解決策を見つけようとしましたが、itertoolsやサイクル機能に遭遇したことはありません。間違いなくそれを将来も利用します。 – svantem

関連する問題