2016-12-03 42 views
0

我正在製作一個Python程序,它是一個通過鬼屋的冒險遊戲。其中最大的一件事就是名爲owned_items的清單。當他們在房子中找到它們或者在開始時被random.choice給予它們時,這將獲得由諸如「匹配框」或「火炬」之類的字符串表示的項目。有時,當他們面臨情況時,他們給出的選擇取決於他們是否有特定的項目。Python:在沒有運行的if語句中打印

我的代碼:

#bullets is the variable for pistol ammo. During my testing of this function, it is at 5 when it should start 
bullets=5 

owned_items=[] 
ronald_items=["a glass bottle", "a pistol", "a torch", "a small glass of  oil", "a box of matches", "a can of spray paint", "a small knife", "a pair of  surgical gloves", "a blessed amulet"] 
owned_items.append(random.choice(ronald_items)) 
ronald_items.remove(owned_items[0] 
owned_items.append(random.choice(ronald_items)) 
ronald_items.remove(owned_items[1]) 


#This part is in the actual definition where the problem appears when it should run 
def skeleton_choice(): 
    if "a glass bottle" in owned_items: 
     print('type "glass bottle" to attack the skeletons with that bottle') 
    if "a pistol" in owned_items and bullets>1: 
     print('type "shoot" to try firing your pistol at the skeletons') 
    if "a small knife" in owned_items: 
     print('type "small knife" to use your small knife against the skeletons') 
    if "a kitchen knife" in owned_items: 
     print('Type "kitchen knife" to use your kitchen knife against the skeletons') 
    if "a blessed amulet" in owned_items: 
     print('Type "amulet" to use the blessed amulet to make this a fairer fight') 
    print('Type "hands" to just fight the skeletons with your body') 
    print('Type "run" to try and get away from the skeletons') 

即使當我知道我的項目3在這些if語句,沒有打印的出現。我使用的是ifs而不是elifs和其他,因爲我希望它能夠顯示所有內容,而不僅僅是一個。例如,如果他們有一個玻璃瓶和一把菜刀,我想讓它給他們打印瓶子和刀子的聲明。

+0

如果這是您的完整代碼並忽略'def skeleton_choice()'下的縮進錯誤,那麼您不會調用該函數,因此它不會運行。 – roganjosh

+1

看起來像你的函數有一個不正確的意圖,並沒有像'def skeleton_choice(input)' – Bryan

+0

這樣的輸入我不知道這是與問題或我的失敗的複製和粘貼,但如果是符合骨骼的sk,而不是def。我看到的縮進沒有任何問題。 –

回答

1

你還沒有在任何地方調用函數,所以這就是爲什麼它不起作用。 只需添加:

skeleton_choice() 

行結束。同樣在行

ronald_items.remove(owned_items[0] 

你缺少一個括號。

+0

缺少的括號只是在問題中,而不是實際的程序(我不信任在縮進問題後複製+粘貼),結果證明我是有史以來最愚蠢的人。我正在修改它如何做骨架的事情。當我決定爲它定義一個函數時,我忘記了實際上使函數運行。它現在完美。 –

0

你的代碼適合我。那是在我將這個調用添加到skeleton_choice()之後的。可能是你只是不叫它?