Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/flash/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用随机模块在三个字符串之间进行选择? 这个程序在我的画布中间画了30个同心圆,我试图用一个我必须调用的函数使每个圆变成一个随机的颜色,我真的不知道为什么这不起作用。你能告诉我:A.为什么现在不这样?B.我怎样修理它 from tkinter import * from random import * root = Tk() w, h = 800, 600 c = Canvas(root, width=w, height=h, bg="gold") c.pack()_Python_Tkinter - Fatal编程技术网

Python 如何使用随机模块在三个字符串之间进行选择? 这个程序在我的画布中间画了30个同心圆,我试图用一个我必须调用的函数使每个圆变成一个随机的颜色,我真的不知道为什么这不起作用。你能告诉我:A.为什么现在不这样?B.我怎样修理它 from tkinter import * from random import * root = Tk() w, h = 800, 600 c = Canvas(root, width=w, height=h, bg="gold") c.pack()

Python 如何使用随机模块在三个字符串之间进行选择? 这个程序在我的画布中间画了30个同心圆,我试图用一个我必须调用的函数使每个圆变成一个随机的颜色,我真的不知道为什么这不起作用。你能告诉我:A.为什么现在不这样?B.我怎样修理它 from tkinter import * from random import * root = Tk() w, h = 800, 600 c = Canvas(root, width=w, height=h, bg="gold") c.pack(),python,tkinter,Python,Tkinter,这就是我需要帮助的部分: def colors(r = "Red", p = "Purple", b = "Blue"): for i in range(0, 3): choice([r, p, b]) colors("Red","Purple","Blue") 还需要调用函数的帮助 cx, cy = w//2, h//2 z = 5 f

这就是我需要帮助的部分:

def colors(r = "Red", p = "Purple", b = "Blue"):
    for i in range(0, 3):
        choice([r, p, b])


colors("Red","Purple","Blue")
    
还需要调用函数的帮助

cx, cy = w//2, h//2
z = 5
for _ in range(30):
    c.create_oval(cx-z, cy-z, cx+z, cy+z, width=2, outline = colors(r, p, b))                                                           
    z += 5
非常感谢

完整代码:

from tkinter import *
from random import *
root = Tk()

w, h = 800, 600
c = Canvas(root, width=w, height=h, bg="gold")
c.pack()


def random_colors(r = "Red", p = "Purple", b = "Blue"):
    for i in range(0, 3):
        choice([r, p, b])


random_colors("Red","Purple","Blue")
    
cx, cy = w//2, h//2
z = 5
for _ in range(30):
    c.create_oval(cx-z, cy-z, cx+z, cy+z, width=2, outline = choice([r,p,b])
z+= 5

root.mainloop()

让我们试试
random.choice

In [28]: from random import *
    ...:
    ...: def random_colors(r, p, b):
    ...:     r = "Red"
    ...:     p = "Purple"
    ...:     b = "Blue"
    ...:     for i in range(0, 3):
    ...:         print(choice([r, p, b]))
    ...:
    ...: random_colors("Red","Purple","Blue")
Purple
Red
Red
你在寻找默认值吗

from random import *

def random_colors(r = "Red", p = "Purple", b = "Blue"):
    for i in range(0, 3):
        print(choice([r, p, b]))

random_colors("Red","Purple","Blue")

我认为你可以通过传递你自己的颜色作为参数来改善这一点

from tkinter import *
from random import choice

root = Tk()

w, h = 800, 600
c = Canvas(root, width=w, height=h, bg="gold")
c.pack()

def colors(*args):
    default = ['red','purple','blue'] # main set of colors
    args = [_ for _ in args] # custom passed color
    color_lst = args + default # list of all colors passed and default
    color = choice(color_lst) #choosing a random color from the list
    return color #returning the color
    
cx, cy = w//2, h//2
z = 5
for _ in range(30):
    c.create_oval(cx-z, cy-z, cx+z, cy+z, width=2, outline = colors('orange','white'))  #calling the function with extra 2 colors                                                          
    z += 5

root.mainloop()

我已经用注释解释了它,以使您在阅读时理解它。另外,从随机导入选项中选择
导入随机
更好,因为否则它可能会污染您的命名空间。

范围(0,3)
等于
范围(3)
,如果不使用它,最好使用
作为变量名。你也可以使用tuple
(r,p,b)
而不是list。所以我问的问题不够好,我编辑了它,你现在可以看一下吗?
范围(0,3)
的目的是什么,它只会打印3种随机颜色。只需将
random.randint(r,p,b)
更改为random.choice((r,p,b))`.thx!我在上面用了你的评论^现在效果很好!希望这有助于@Cool Cloud lololok谢谢。看起来我好像在试图获得“一天三十次投票”的徽章,因为我仍然被认为是一个傻瓜。。。但不用担心,我不会再这样做了