如何从列表中的值更改乌龟的笔颜色?

时间:2017-05-19 00:24:12

标签: python turtle-graphics

我有一个乌龟绘制的数字列表,但我希望笔颜色根据列表中数字的值进行更改。

colors = ["red", "orange", "green", "cyan", "blue", "purple", "magenta", "pink"]

digits = map(int,str(5))

for number in digits:
    *code to change pen color to value of "index(number)" (in this case purple)*
    t.right(number*10)

仅使用5作为示例,有没有办法根据列表中元素的值更改颜色?

我对Python有点新意,所以感谢您提前提供任何帮助。

1 个答案:

答案 0 :(得分:0)

您可以通过访问它的.color方法来更改乌龟的颜色,如果它只是您要更改的笔颜色,则可以将.color替换为.pencolor

colors = ["red", "orange", "green", "cyan", "blue", "purple", "magenta", "pink"]

for number in range(5):
    t.color(colors[number])
    t.right(number*10)

或者更好的是,使用enumerate

colors = ["red", "orange", "green", "cyan", "blue", "purple", "magenta", "pink"]
for number, color in enumerate(colors):
    t.color(color)
    t.right(number*10)