首页 > 解决方案 > 输入中的拼写错误仍然符合在 UNIX 上使用 Python 2.7 进行基于文本的 RPG 游戏的条件

问题描述

一个月以来,我一直在使用“Learn Python The Hard Way”教程学习如何编码。到目前为止,这很有趣。我挑战自己创建基于文本的 RPG 游戏。

我目前正在重写代码以提高可读性和维护性。

我面临一个问题:

如果我输入选择输入“open heaby door”(注意拼写错误),条件仍然满足,我得到预期的打印,就好像它没有拼写错误一样。

关于我做错了什么的任何想法?

谢谢您的帮助 :)

PS:我还有另一个问题,但我想我必须在单独的问题文件中提问。

prison_key = False
def test():
    global prison_key
    curr_prompt = "What do you do?"
    print curr_prompt
    choice = raw_input("> ").lower()
    while "quit" not in choice:
        if "go" in choice:
            if "cellar" in choice:
                print "cellar"
            elif "gravel" in choice or "path" in choice:
                print "gravel path"
            elif "prison" in choice and prison_key:
                print "You enter the prison."
            elif "prison" in choice: 
                print "The door is locked."
            else:
                print "Invalid"
        elif "search" in choice:
            if "search" == choice:
                print "invalid"
            elif "prison":
                print "The door is closed."
            elif "bucket" in choice:
               print "The bucket is empty."
            elif "keg" in choice:
                prison_key = True
                print "You find a key."
            elif "door" in choice and ("heavy" in choice or "steel" in choice or "metal" in choice):
                print "It looks like a prison door"
            elif "search door" == choice:
                print "Which one?"
            else:
                print "You find nothing."
        elif "open door" == choice:
                print "which one?" 
        elif "open" in choice:
            if "door" in choice and ("wooden" in choice or "cellar" in choice):
                print "gravel path"
            elif "door" in choice and ("steel" in choice or "metal" in choice or "door" in choice or "heavy" in choice or "prison" in choice) and prison_key:
                print "You open the prison"
            elif "door" in choice and ("steel" in choice or "metal" in choice or "door" in choice or "heavy" in choice or "prison" in choice):
                print "the door is locked"
            elif "prison" in choice and prison_key:
                print "You enter the prison."
            elif "prison" in choice:
                print "the door is locked."
            else:
                print "invalid"
        elif "drink" in choice and "wine" in choice:
            print "You alcoholic."
        else:
            print "invalid"
        print curr_prompt
        choice = raw_input("> ").lower()
    exit(0)

test()

标签: pythonif-statementinputwhile-looptext-based

解决方案


所以发现你的问题。在 elif 条件下

elif "door" in choice and ("steel" in choice or "metal" in choice or "door" in choice or "heavy" in choice or "prison" in choice):

您正在再次检查门,因此它通过了 if 条件。同样适用于这种情况

elif "door" in choice and ("steel" in choice or "metal" in choice or "door" in choice or "heavy" in choice or "prison" in choice) and prison_key:                 

    print "the door is locked"

推荐阅读