簡體   English   中英

你能用一個列表在while循環中改變烏龜筆的顏色嗎?

[英]Can you change the color of the turtle pen in a while loop using a list?

我想知道你是否可以在while循環迭代時更改python烏龜筆的顏色,並在每次while循環迭代時繼續更改。 到目前為止,我有這個,但也想讓它改變顏色。

from turtle import *

w = 10
# Number of steps to take
x = 40
# Number of sides to make
y = 2
# Size of turtle pen
z = x
# Same variable as x but not manipulated
speed ('fastest')
# Speed of pen 

    for i in range(10):
        while x > 0:
            pensize (y)
            forward (w)
            left (360/z)
            x = x-1
            y = y+1
        while x < z:
            pensize (y)
            forward (w)
            right (360/z)
            x = x+1
            y = y-1
        print (i)

有什么建議么?

有一個pencolor功能,所以,是的,你可以改變顏色。

如果你的問題不是烏龜,而是從一個太短的列表中優雅地取值,我建議使用itertoolsnext()方法。

from itertools import cycle
color_sequence = cycle([4,5,6])
for i in range(10): print color_sequence.next()

如果你不想為每個筆划都有一個額外的命令,你只需通過調用你自己的list_colored_forward()來替換對forward()的調用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM