2017-10-09 25 views
0

我试图生成随机的句子,但它不工作,我已经导入随机的,这是我所:随机功能将无法正常工作

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.") 
+3

你会得到什么错误? – jacg

+0

在这种情况下,您可以使用'else:'而不是'elif',您当前的代码应该可以工作 –

+0

我没有收到错误消息,它只是重复“您为自己做了什么?每一次 – John

回答

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.") 
+0

啊它没有工作:(它只是做同样的事情,并重复“你把自己的什么?” – John

+0

@约翰我运行它时正常工作,请再试一次 – Ajax1234

+0

它仍然无法正常工作。前面的代码是: elif decision ==“在梳子上使用剃须刀”或“在瓶子上使用剃须刀”或“在洗手盆上使用剃须刀”或“在厕所上使用剃须刀”或“在监狱酒吧上使用剃须刀”或“使用剃须刀在锁定机制“: – John