在两个选项之间随机选择

时间:2020-07-20 01:58:44

标签: python python-2.7 if-statement random

我是python的新手,正在尝试更好地学习该语言。我正在尝试创建非常简单的内容,正在使用 if 语句,我正在尝试选择随机选项,并根据选择的内容做出不同的响应。我看起来像这样...

<img src="/getLinearImage" alt="User Image" height="100px" width="100px">

但是结果总是猫。我如何使其随机选择?

1 个答案:

答案 0 :(得分:8)

在列表上使用random模块和choice方法

import random

species_list = ["cat", "dog"]
species = random.choice(species_list)
if species == "cat":
    print("yep, it's a cat")
else:
    print("nope, it's a dog")
相关问题