Reputation: 51
It's coded to go from 100
to 0
radius circles one by one like red, blue, red, blue. I did it with if else but I am sure I can do that in a more advanced way like random,lists or whatever. Can you help with that?
import turtle
screen = turtle.Screen()
turtle = turtle.Turtle('turtle')
turtle.pensize(3)
turtle.pencolor('red')
def circle(x, y, r):
if r <= 0:
return
turtle.penup()
turtle.goto(0, -r)
turtle.pendown()
turtle.circle(r)
if (turtle.pencolor() == 'red'):
turtle.pencolor('blue')
else:
turtle.pencolor('red')
circle(0, 0, r-10)
circle(0, 0, 100)
screen.exitonclick()
Upvotes: 1
Views: 96
Reputation: 14823
Instead of 4 lines for if-else, you can use a ternary operator to set color in one line:
color = "red" if turtle.pencolor() == "blue" else "blue"
turtle.pencolor(color)
It saves a few lines but it doesn't really optimize anything compared to yours.
Upvotes: 0
Reputation: 73460
You can use itertools.cycle
to, well, cycle through the colors:
from itertools import cycle
colors = cycle(["red", "blue"])
# ...
def circle(...):
turtle.pencolor(next(colors)) # will assign alternating values
# ...
Upvotes: 5