如何将随机颜色添加到随机圆形

问题描述:

每次运行代码时,我都需要为不同大小的圆圈填充不同的颜色。如何将随机颜色添加到随机圆形

from graphics import* 
from random import* 
from time import* 

circle_x=0 
circle_y=0 
colors =0 

#Graphics Window 
def main(): 
    win = GraphWin("Bubbles", 500,500) 
    message = Text(Point(250,200),"Click anywhere to continue") 
    message.draw(win) 
    win.getMouse() 
    message.undraw() 
main()  

#Create Circle 
def create(): 
    win = GraphWin("Bubbles", 500,500) 
    for i in range (4): 

    # Creating a random point for the x of the circle 
     circle_x = randint(50,450) 

    #Creating a random point for the y of the circle 
     circle_y = randint(0,100) 

     p = Point(circle_x,circle_y) 

     radius_x = randint(3,20) 
     c = Circle(p,radius_x) 

     colors = ("salmon","red","blue","green","purple","orange","yellow") 
     fill = choice (colors) 

     c.draw(win) 

我已经远远做到了这一点,但不知何故,颜色没有得到填充。 我需要使用choice

+2

对不起,但是:为什么当你可以称它为'create_circle'时,你在上面评论过你的函数? – byxor

+0

在您的代码中包含您的导入。你是从'random'模块,'turtle'模块导入的吗? 你做过独家进口吗? – 0TTT0

+0

@ 0TTT0我编辑过它。我不喜欢用乌龟。 –

colors = ("salmon","red","navy","steelblue","wheat","darkorange","yellow") 
     fill = choice (colors) 
     c.setFill(fill) 
     c.draw(win)  

解决了问题!