python - 类型错误 : 'in <string>' requires string as left operand not list

标签 python

我正在完成 Learn Python The Hard Way (LPTHW) 的练习 35。

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

我正在为本练习的一项学习练习而苦苦挣扎。特别是“为游戏添加更多内容”。

这是返回错误的代码:

def bear_room(): # A new encounter
    print "There is a bear here."
    print "The bear has a bunch of honey."
    print "The fat bear is in front of another door."
    print "How are you going to move the bear?"

    bear_moved = False 

while True: 
    next = str(raw_input("> "))

    if ["honey", "take"] in next: 
        dead("The bear peers at you for a moment, sizing you up, then claws your face off.")
    elif ["taunt", "lure", "yell", "scream", "shout"] in next:
        print "the bear has moved from the door. You can go through it now."
        bear_moved = True
    elif ["taunt", "lure", "yell", "scream", "shout"] in next and bear_moved: 
        dead("The bear gets pissed off and chews your leg off.")
    elif ["open", "door", "next", "through", "onward"] in next and bear_moved:
        gold_room()
    else:
        print "I got not idea what that means."

最佳答案

next in ["taunt", "lure", "yell", "scream", "shout"]

不是

["taunt", "lure", "yell", "scream", "shout"] in next

你的测试倒退了。

关于python - 类型错误 : 'in <string>' requires string as left operand not list,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19557480/

相关文章:

python - 合并多个 DataFrame

python - 在 .INI 文件中将变量值更改为 float

python - 为什么使用 Python 生成器遍历二叉树比不使用慢得多?

python - 无法使用 pymssql 连接到 mssql 数据库

python - 如何检查字典列表是否已排序?

Python/Tkinter : Using custom mouse cursors under Windows?

python - "settings.DATABASES is improperly configured"与 django 1.6

python - 参数必须是字节或unicode,得到 '_Element'

python - UDP数据包加密

python - 值错误 : negative dimensions are not allowed