有没有另一种方法可以分别选择蓝色和红色?

Yok*_*tık 1 python python-turtle

它的编码从去1000一个个像红色,蓝色,红色,蓝色半径的圆。我用 if else 做到了,但我相信我可以用更高级的方式来做到这一点,比如随机、列表或其他任何东西。你能帮忙吗?

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()
Run Code Online (Sandbox Code Playgroud)

sch*_*ggl 5

您可以使用itertools.cycle来循环显示颜色:

from itertools import cycle

colors = cycle(["red", "blue"])

# ...
def circle(...):
    turtle.pencolor(next(colors))  # will assign alternating values
    # ...
Run Code Online (Sandbox Code Playgroud)