Python中的随机函数不起作用

时间:2017-10-09 14:24:20

标签: python random

我正在尝试生成随机句子,但它不起作用,我已经随机导入,这就是我所拥有的:

    if "razor blade" in inventory:
        sentences = ("What do you take yourself for?","Are you insane?", "You are not doing that.", "You have to be joking.", "This isn't going to work.")
        num= random.randrange (0,5)
        print (sentences[num])
    elif "razor blade" not in inventory:
        print ("you don't own this item in your inventory.") 

1 个答案:

答案 0 :(得分:1)

您可以使用random.choice

import random
if "razor blade" in inventory:
    sentences = ("What do you take yourself for?","Are you insane?", "You are not doing that.", "You have to be joking.", "This isn't going to work.")
    print(random.choice(sentences))
elif "razor blade" not in inventory:
    print ("you don't own this item in your inventory.")