我正在使用turtle python,我正在尝试做类似的事情:
我做了两种颜色的代码,这基本上很简单。问题是我不知道如何定义三个或更多的交替颜色。这是我的代码:
imp = 0
while imp != 5:
wn = turtle.Screen()
Cicle = turtle.Turtle()
Cicle.speed(30)
print("1) Use just one color (Default orange)")
print("2) Use two colors (Default: red and blue)")
print("3) Use three colors (Default: red, blue and orange)")
print("4) Use four colors (Default: red, blue, yellow, black)")
print("5) If you want to exit")
imp = eval(input("Make your choice: "))
number = eval(input("How many cycles do you want to draw? "))
radius= eval(input("Define the radius: "))
if imp == 1:
for x in range(number):
Cicle.color("orange")
Cicle.circle(radius)
Cicle.right(int(360/number))
if imp == 2:
wn = turtle.Screen()
Cicle = turtle.Turtle()
Cicle.speed(15)
for x in range(number):
if x % 2 == 0:
Cicle.color("blue")
else:
Cicle.color("red")
Cicle.circle(radius)
Cicle.right(int(360/number))
if imp == 3:
wn = turtle.Screen()
Cicle = turtle.Turtle()
Cicle.speed(15)
for x in range(number):
if x:
Cicle.color("red")
if x + 1:
Cicle.color("blue")
if x + 2:
Cicle.color("orange")
Cicle.circle(radius)
Cicle.right(int(360/number))
你能帮助我吗?
答案 0 :(得分:2)
您可以尝试使用颜色列表并存储用户可以覆盖的一些默认颜色。
然后您可以使用与代码中相同的两种颜色原则:使用模数操作选择颜色。如果列表中有颜色,则可以使用模运算的结果作为列表的索引:
if imp == 3:
wn = turtle.Screen()
Cicle = turtle.Turtle()
Cicle.speed(15)
colors = ['red', 'green', 'blue']
for x in range(number):
c = colors[x % 3]
Cicle.color(c)
Cicle.circle(radius)
Cicle.right(int(360/number))