随机是/否生成python

hel*_*elp 2 random python-3.x

我知道如果我想随机生成一个数字,我会做这样的事情

import random
run = -1
for x in range(10):
    rand = random.randint(1, 30)
Run Code Online (Sandbox Code Playgroud)

但是我怎样才能随机生成是或否而不是数字呢?

Bil*_*ell 7

您可以非常直接地choice从标准模块中完成random

>>> from random import choice
>>> answer = choice(['yes', 'no'])
>>> answer
'yes'
Run Code Online (Sandbox Code Playgroud)