0

所以为了上课,我正在制作一个糟糕的文字冒险游戏。我想根据我的可接受单词词典检查用户输入的内容。但是,当我执行以下操作时,我得到一个'TypeError:'set' object is not subscriptable'。我将如何解决这个问题?

“游戏”的一小段代码:

def butts():
    southLib={"long thing", "rough thing", "cylinder thing", "floor thing", "home"}

    userPoop = str(input("Would you like to poop in the long thing, rough thing, cylinder thing, floor thing, or home? None?"))

    while southLib[userPoop] == None:
        print("I do not understand")
        userPoop = str(input("Would you like to poop in the long thing, rough thing, cylinder thing, floor thing, or home? None?"))

butts()
4

2 回答 2

0

如果您想检查某些东西是否不在集合中,只需使用not in运算符:

>>> my_set = {"foo", "bar", "baz"}
>>> "foo" not in my_set
False
>>> "qux" not in my_set
True

(类似地,您可以使用in来检查某物是否在集合中。)

于 2013-10-16T06:30:33.310 回答
0

进行适当的收容检查。

while userPoop not in southLib:
于 2013-10-16T06:30:01.047 回答