我需要为每个新的小方块添加新的随机颜色。现在它只在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)
答案 0 :(得分:0)
我知道你解决了你的问题,但random.choice()更好 适合你的功能:
我不同意。 RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# for hello world first page we are giving static value 1 to page.
RewriteRule ^([\w-]+)/?$ index.php?url=$1&page=1 [QSA,L]
# for others we are getting dynamic value.
RewriteRule ^([\w-]+)/([\d]+)$ index.php?url=$1&page=$2 [QSA,L]
的问题是您可以在连续的调用中获得相同的颜色,这是您不想要的:
random.choice()
将>>> import random
>>> COLORS = ['red', 'blue', 'green', 'yellow', 'black', 'pink', 'gold', 'violet', 'orange', 'magenta', 'cyan']
>>> for _ in range(10):
... print(random.choice(COLORS))
...
green
pink
red
black
violet
orange
orange
violet
yellow
yellow
>>>
与random.shuffle()
结合使用,可以为您提供一个随机序列的颜色,这些颜色可以重复使用,您可以一个接一个地采用不同的颜色:
itertools.cycle()
答案 1 :(得分:-1)
我知道您解决了问题,但 random.choice()更适合您的功能:
def random_color():
L=['red','blue','green','yellow','black','pink','gold','violet','coral','lemon', 'chiffon','seagreen']
return random.choice(L)
答案 2 :(得分:-1)
imputturtlegraphics,
setpcolor:("red,green,blue,gold,puple,maroon,claret,tuquoise etc")
,pendown
,goto("x$y$")
,movehundredstepsforward
,turnleft90
repeatprev2sX4