2016-12-03 114 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()之后的。可能是你只是不叫它?