在系列中选择一个随机数(python) [英] Selecting a random number within a series(python)
问题描述
我正在尝试创建一个程序,从一系列 2 个数字中选择一个随机数,有谁知道这是如何完成的?我查了一下,说要使用选择函数,但是出现了一个错误,说当代码只需要 2 个参数时,我给出了 3 个参数.
I'm trying to create a program that picks a random number out of a series of 2 numbers, does anyone know how this is done? I looked it up and said to use the choice function, however an error came up saying I had given 3 arguments when the code needed only 2.
我该怎么做才能让它在一个系列中选择一个随机数?
What do I have to do to make it pick a random number in a series?
variable = [choice(a,b),choice(a,b)]
variable = choice(1,5,9,27) # variable should now = 1, 5, 9 or 27.
推荐答案
As 文档说明,只需写choice((1, 5, 9, 27))
.
如果你写choice(1, 5, 9, 27)
,那就是向函数传递四个参数.
If you write choice(1, 5, 9, 27)
, that is passing four arguments to the function.
如果你写choice((1, 5, 9, 27))
,那就是传递一个参数,一个由四个元素组成的元组,(1, 5, 9, 27)
, 到函数.
If you write choice((1, 5, 9, 27))
, that is passing a single argument, a tuple of four elements, (1, 5, 9, 27)
, to the function.
这篇关于在系列中选择一个随机数(python)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!