【发布时间】:2026-01-18 21:20:02
【问题描述】:
我需要为每个新的小方块添加一个新的随机颜色。现在它只在 while 循环后改变颜色。你能帮我修一下吗?
def random_color():
colvar=random.randint(0,10)
L=['red','blue','green','yellow','black','pink','gold','violet','coral','lemon chiffon','sea green'] #wiki.tcl.tk/16166 website which I used to find names for colors
result=L[colvar]
return result
def square(color,x,y):
turtle.color(color)
turtle.begin_fill()
turtle.penup()
turtle.goto(x,y)
turtle.pendown()
line1=200 #creates a new smaller square
while line1>=10:
line1=line1-10
for i in range(2):
turtle.forward(line1)
turtle.right(90)
turtle.forward(line1)
turtle.right(90)
def drawsqr():
num=5
for i in range(num):
color=random_color() #change color after each complete cycle
x=250
y=250
square(color,x,y)
【问题讨论】: