Python(LPTHW)练习36

时间:2015-12-25 07:43:59

标签: python

我是编程的新手,正在学习Python学习Python的艰难之路。我正在练习36,我们被要求编写自己的简单游戏。

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

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

我做错了什么?)

编辑:我不应该在选择中写出'if“oldman”或“man”,而是在每个选项之后选择“选择”。

然而,接下来的问题......男人从不站起来诅咒我,它一直皱着眉头。为什么游戏没有进入那个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?"

感谢您的帮助:)。

2 个答案:

答案 0 :(得分:3)

仔细查看您的条件评估:

    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" ]:          

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

答案 1 :(得分:2)

  

问题是,当我在走廊里时(或者更准确地说,是在走廊里)   '选择')我写'门',游戏回应就像我说的“男人”   “老人”等。

说明

你所做的是被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