使用choice()和randint()从2个范围中获取1个数字 [英] Getting 1 number from 2 ranges with choice() and randint()

查看:423
本文介绍了使用choice()和randint()从2个范围中获取1个数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个代码块

import random as r
value = r.choice(r.randint(10, 30), r.randint(50, 100))
print (value)

为什么会给我以下错误,我该如何解决?

Why does it give me the following error, and how should I fix it?

TypeError: choice() takes 2 positional arguments but 3 were given 

推荐答案

choice可以选择一系列项目,而不是varargs.将参数包装成匿名tuplelist,它将起作用,并进行以下更改:

choice takes a sequence of items to choose from, not varargs. Wrap the arguments to make an anonymous tuple or list and it'll work, changing:

value = r.choice(r.randint(10, 30), r.randint(50, 100))

到以下任何一个

value = r.choice((r.randint(10, 30), r.randint(50, 100)))  # Tuple
value = r.choice([r.randint(10, 30), r.randint(50, 100)])  # List

这篇关于使用choice()和randint()从2个范围中获取1个数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆