2017-10-09 45 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