Python:在if语句未运行时打印

时间:2016-12-03 11:19:19

标签: python list if-statement printing in-operator

我正在制作一个python程序,这是一个通过鬼屋的冒险游戏。其中一个重要的事情是名为owned_items的列表。这将获得由字符串表示的项目,例如"匹配框"或者"火炬"当他们在房子里找到它们或者在random.choice开始时给它们。有时,当他们遇到某种情况时,他们给出的选择取决于他们是否有特定的项目。

我的代码:

#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')

即使我知道这些if语句中有3个项目,也不会显示任何打印件。我使用ifs而不是elifs和其他因为我希望它显示他们拥有的所有内容,而不仅仅是一个。例如,如果他们有一个玻璃瓶和一把菜刀,我希望它能给他们瓶子和刀子的打印声明。

2 个答案:

答案 0 :(得分:1)

您没有在任何地方调用该函数,因此它无法正常工作。 只需添加:

skeleton_choice()

最后一行。也在行

ronald_items.remove(owned_items[0]

你错过了一个括号。

答案 1 :(得分:0)

您的代码适合我。在我添加对skeleton_choice()的调用之后。你可能只是不称呼它?