我有一个乌龟绘制的数字列表,但我希望笔颜色根据列表中数字的值进行更改。
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有点新意,所以感谢您提前提供任何帮助。
答案 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)