简体   繁体   English

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

[英]How do you change the turtle's pen color from a value in a list?

I have a list of numbers that the turtle draws, but I want the pen color to change based upon the value of the number in the list. 我有一个乌龟画的数字列表,但我希望根据表中数字的值来改变笔的颜色。

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)

Just using 5 as an example, is there a way to change the color based on the value of an element in the list? 仅以5为例,有没有一种方法可以根据列表中元素的值更改颜色?

I'm a bit new to Python, so thank you for any help in advance. 我对Python有点陌生,因此,感谢您提前提供的帮助。

You can change a turtle's color by accessing it's .color method, if it's only the pen color you want to change, you can replace .color with .pencolor : 您可以通过访问.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)

Or better yet, use enumerate : 或更妙的是,使用enumerate

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM