Getting 1 number from 2 ranges with choice() and randint()

后端 未结 2 1555
慢半拍i
慢半拍i 2021-01-23 02:13

I have a block of code with

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

Why does it give me the fo

2条回答
  •  余生分开走
    2021-01-23 02:41

    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))
    

    to either of:

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

提交回复
热议问题