类型错误:'in <string>' 需要左操作数为字符串而不是列表。

4

我正在完成《笨办法学Python》(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."
1个回答

8

你需要将代码中的测试条件改为:

next in ["嘲讽", "引诱", "喊叫", "尖叫", "呼喊"]

而不是

["嘲讽", "引诱", "喊叫", "尖叫", "呼喊"] in next

你的测试条件是反过来的。


非常感谢,已经测试并确认可行!它们对我来说仍然看起来一样,显然它们不是一样的,但没关系! - Harrison Boles
2
在这种情况下,顺序很重要,“钥匙在锁里”和“锁在钥匙里”是不同的。另外,请记得将答案标记为正确。 - SpliFF

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接