我正在製作一個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和其他,因爲我希望它能夠顯示所有內容,而不僅僅是一個。例如,如果他們有一個玻璃瓶和一把菜刀,我想讓它給他們打印瓶子和刀子的聲明。
如果這是您的完整代碼並忽略'def skeleton_choice()'下的縮進錯誤,那麼您不會調用該函數,因此它不會運行。 – roganjosh
看起來像你的函數有一個不正確的意圖,並沒有像'def skeleton_choice(input)' – Bryan
這樣的輸入我不知道這是與問題或我的失敗的複製和粘貼,但如果是符合骨骼的sk,而不是def。我看到的縮進沒有任何問題。 –