【问题标题】:TypeError: 'in <string>' requires string as left operand not listTypeError: 'in <string>' 需要字符串作为左操作数而不是列表
【发布时间】:2013-10-24 05:14:16
【问题描述】:

我正在完成 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."

【问题讨论】:

    标签: python


    【解决方案1】:

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

    不是

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

    你的测试倒退了。

    【讨论】:

    • 非常感谢测试并确认可以工作!在我看来,它们仍然是一样的,显然它们不是但还可以!
    • 在这种情况下顺序很重要,“钥匙在锁中”与“锁在钥匙中”不同。另外,记得将答案标记为正确。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-08
    • 1970-01-01
    • 1970-01-01
    • 2022-08-11
    • 2021-08-22
    • 1970-01-01
    • 2021-07-29
    相关资源
    最近更新 更多