首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Python:在不运行的if语句中打印

Python:在不运行的if语句中打印
EN

Stack Overflow用户
提问于 2016-12-03 11:19:19
回答 2查看 72关注 0票数 0

我正在制作一个蟒蛇程序,这是一个冒险游戏的东西,通过鬼屋。其中最重要的事情之一是一个名为owned_items的列表。这会得到一些字符串表示的项目,比如,“火柴盒”或“火炬”,当他们在房子里找到它们时,或者在开始时由random.choice提供给它们。有时,当他们遇到某种情况时,他们得到的选择取决于他们是否有特定的物品。

我的代码:

代码语言:javascript
运行
复制
#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和一个其他的,因为我希望它能显示他们拥有的一切,而不仅仅是一个。例如,如果他们有一个玻璃瓶和一个菜刀,我希望它给他们打印声明的瓶子和刀。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-12-03 12:09:37

您还没有在任何地方调用该函数,这就是它不能工作的原因。只需添加:

代码语言:javascript
运行
复制
skeleton_choice()

最后排好队。也在排队

代码语言:javascript
运行
复制
ronald_items.remove(owned_items[0]

你少了一个括号。

票数 1
EN

Stack Overflow用户

发布于 2016-12-03 12:09:25

你的密码对我有用。在我将调用添加到skeleton_choice()之后。可能是你不这么说吗?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40947165

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档