【问题标题】:Python (LPTHW) Exercise 36 [duplicate]Python (LPTHW) 练习 36 [重复]
【发布时间】:2016-03-31 09:12:03
【问题描述】:

我是编程新手,我正在通过《Learning Python the Hard Way》一书学习 Python。我在练习 36,要求我们编写自己的简单游戏。

http://learnpythonthehardway.org/book/ex36.html

(问题是,当我在走廊(或更准确地说,在“选择”中)并且我写“门”时,游戏的反应就好像我说的是“老人”等“男人”。

我做错了什么?)

编辑:我不应该写 'if "oldman" or "man" in choice' 而是在每个选项后加上 'inchoice'。

然而,下一个问题.. 这个人从不站起来骂我,它一直皱着眉头。为什么游戏没有进行到那个elif?

experiences = []    
def choices():
        while True:
            choice = raw_input("What do you wish to do? ").lower()

            if "oldman" in choice or "man" in choice or "ask" in choice:
                print oldman
            elif "gate" in choice or "fight" in choice or "try" in choice and not "pissedoff" in experiences:
                print "The old man frowns. \"I said, you shall not pass through this gate until you prove yourself worthy!\""
                experiences.append("pissedoff") 
            elif "gate" in choice or "fight" in choice or "try" in choice and "pissedoff" in experiences and not "reallypissed" in experiences:
                print "The old man is fed up. \"I told you not to pass this gate untill you are worthy! Try me again and you will die.\""
                experiences.append("reallypissed")

            elif "gate" in choice or "fight" in choice or "try" in choice and "reallypissed" in experiences:
                print "The old man stands up and curses you. You die"
                exit()


            elif "small" in choice:
                print "You go into the small room"
                firstroom()
            else: 
                print "Come again?"

编辑:已修复!!

def choices():
    while True:
        choice = raw_input("What do you wish to do? ").lower()

        if choice in ['oldman', 'man', 'ask']:
            print oldman
        elif choice in ['gate', 'fight', 'try'] and not 'pissedoff' in experiences:
            print "The old man frowns. \"I said, you shall not pass through this gate until you prove yourself worthy!\""
            experiences.append("pissedoff") 
        elif choice in ['gate', 'fight', 'try'] and not 'reallypissed' in experiences:
            print "The old man is fed up. \"I told you not to pass this gate untill you are worthy! Try me again and you will die.\""
            experiences.append("reallypissed")
        elif choice in ['gate', 'fight', 'try'] and 'reallypissed' in experiences:
            print "The old man stands up and curses you. You die"
            exit()
        elif "small" in choice:
            print "You go into the small room"
            firstroom()
        else: 
            print "Come again?"

感谢您的所有帮助:)。

【问题讨论】:

  • 我写了三遍“门”,我要让老人站起来骂人。在第一次将“生气”添加到体验列表后,如果我再次输入门它应该警告我并将真的很生气添加到体验列表中,如果我第三次输入“门”它应该退出。
  • 嘿,我有一个建议:您可以使用if choice in [..., ...] 重写 if/elif 以使其更容易理解吗?我一会儿回来看看!
  • 我做到了,我想我修好了!非常感谢您的帮助:)。

标签: python


【解决方案1】:

仔细查看您的条件评估:

    if "oldman" or "man" or "ask" in choice:

这将首先评估"oldman" 是否为True,恰好是这种情况,if 条件将评估为True

我想你的意图是:

    if "oldman" in choice or "man" in choice or "ask" in choice:         

您也可以使用:

    if choice in ["oldman" , "man", "ask" ]:          

需要注意的是,这种方法是在寻找完全匹配而不是子字符串匹配。

【讨论】:

    【解决方案2】:

    问题是,当我在走廊时(或者更准确地说,在 'choices')然后我写下 'gate' 游戏的反应就好像我说的是“人” “老人”等

    说明

    Python 认为你正在做的事情是:

    if ("oldman") or ("man") or ("ask" in choice):
    

    如果这 3 个条件中的 任何"truthy" value,则计算结果为 True。像 "oldman" 和 "man" 这样的非空字符串的计算结果为 True。这就解释了为什么您的代码会如此行事。

    测试一个选项是否在选项列表中:

    你在寻找

    if choice in ['oldman', 'man', 'ask']:
    

    或者,至少:

    if 'oldman' in choice or 'man' in choice or 'ask' in choice
    

    【讨论】:

    • 这就解释了,谢谢:)。我已经编辑了我的问题,我希望这就是我应该这样做的方式。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-31
    • 1970-01-01
    • 2015-06-11
    • 2014-02-18
    • 2020-06-13
    • 1970-01-01
    相关资源
    最近更新 更多