Reputation: 179
I have a list of colors:
colors = ["red", "blue", "green", "yellow"]
and I would like to display text using turtle.write()
to alternate the colors of the letters in the words "DOG" and "ALLIGATOR".
The letters of "DOG" would be colored "red", "blue", and "green"
The letters of "ALLIGATOR" would be colored "red", "blue", "green", "yellow", "red", "blue", "green", "yellow", "red".
How can this be done in Turtle Graphics? Thank you!
Upvotes: 0
Views: 4240
Reputation: 41872
There are a couple of things that should make this easier to implement. The first is to use itertools.cycle()
to repeatedly work through your list of colors. The other is to use the move=True
argument to turtle.write()
so that you can print the individual characters of the words one right after the other:
from turtle import Turtle, Screen
from itertools import cycle
FONT = ('Arial', 36, 'normal')
COLORS = ["red", "blue", "green", "yellow"]
def stripe_write(turtle, string):
color = cycle(COLORS)
for character in string:
turtle.color(next(color))
turtle.write(character, move=True, font=FONT)
yertle = Turtle(visible=False)
yertle.penup()
stripe_write(yertle, "DOG")
yertle.goto(100, 100)
stripe_write(yertle, "ALLIGATOR")
screen = Screen()
screen.exitonclick()
Upvotes: 3