如何使用列表交叉引用输入

How to cross-reference input with list

我正在做一个小的 CLI 冒险游戏。我正在尝试将用户输入的响应与一系列操作进行比较,以确保其有效,但我似乎无法弄清楚如何准确地做到这一点。如果有帮助就更好了!

#Preamble/lore goes above here
print("What do you do: ")
response = input()

list = ["inv","Inv","Inventory","inventory","help","Help","Lamp","lamp","Look","look","Search","search","bed","Bed","Use","use","Drink", "drink","Bucket","bucket","Gate","gate","escape","Escape"]

while True:

#Game Code goes above here
        for x in list:
        if x != response:
            scroll(Fore.RED+ "Invalid Input; please enter again.\n"+Fore.WHITE)
            scroll("What do you do: ")
            response = input()

我希望游戏循环播放事件,以便用户可以返回并执行他们可能错过的操作。假设他们在拿起地图之前搜索了该区域。

一旦用户输入了无效的输入;它不允许他们 return 回到标准输入说他们拼写错误的关键字并尝试第二次拼写正确。它只会不断循环无效的输入消息。

当没有无效输入的错误代码时,我可以正确地将其设置为loop/work;但是如果我在确实发生无效输入时这样做;它只会使应用程序崩溃。

所以我设法让它工作,但以一种真正未优化的方式。但至少它有效。我将列表中的每个项目都转换为“和”检查,不太确定它如何工作或为什么工作,但确实如此。

if response != "inv" and response != "Inv" and response != "Inventory" and response != "inventory" and response != "help" and response != "Help" and response != "Lamp" and response != "lamp" and response != "Look" and response != "look" and response != "Search" and response != "search" and response != "bed" and response != "Bed" and response != "Use" and response != "use" and response != "Drink" and response !=  "drink" and response != "Bucket" and response != "bucket" and response != "Gate" and response != "gate" and response != "escape" and response != "Escape":
        cls()
        scroll(Fore.RED+ "Invalid Input; please enter again.\n"+Fore.WHITE)
        scroll("What do you do: ")
        response = input()
        print(response)