-1

行,

所以我目前正在研究一个简单的文本 rpg(在 Python 中)。但由于某种原因,我的一个功能是读取奇怪的输入。

现在,地牢中的每个房间都是一个独立的功能。这是不工作的房间:

def strange_room():

    global fsm
    global sword
    global saw

    if not fsm:
        if not saw:
            print "???..."
            print "You're in an empty room with doors on all sides."
            print "Theres a leak in the center of the ceiling... strange."
            print "In the corner of the room, there is an old circular saw blade leaning against the wall."
            print "What do you want to do?"

            next6 = raw_input("> ")

            print "next6 = ", next6

            if "left" in next6:
                zeus_room()

            elif "right" in next6:
                hydra_room()

            elif "front" or "forward" in next6:
                crypt_room()

            elif ("back" or "backwad" or "behind") in next6:
                start()

            elif "saw" in next6:
                print "gothere"
                saw = True
                print "Got saw."
                print "saw = ", saw
                strange_room()

            else:
                print "What was that?"
                strange_room()

        if saw:
            print "???..."
            print "You're in an empty room with doors on all sides."
            print "Theres a leak in the center of the ceiling... strange."
            print "What do you want to do?"

            next7 = raw_input("> ")

            if "left" in next7:
                zeus_room()

            elif "right" in next7:
                hydra_room()

            elif "front" or "forward" in next7:
                crypt_room()

            elif ("back" or "backwad" or "behind") in next7:
                start()

            else:
                print "What was that?"
                strange_room()

我的问题是获得我的意见。这个函数一直执行到第 17 行。似乎第一次需要输入,但是打印输入的 print 语句没有执行。然后,除此之外,只有左、右和前/前命令正常工作。我输入的任何其他内容都只会执行“front”/“forward”应该执行的 crypt_room() 函数。

谢谢。

4

2 回答 2

4

表达方式

"front" or "forward" in next6

在语句中计算"front"并始终被认为是正确的。if你可能的意思是

"front" in next6 or "forward" in next6

您的代码中有更多此类错误。一般来说,表达式

A or B

评估AifA真的B否则。

作为旁注,您的程序的整个设计都被破坏了。进入不同房间时的递归调用会很快达到最大递归深度。

于 2012-02-07T15:20:34.867 回答
0

Sven Marnach 说为什么你的代码不起作用。为了使其正常工作,您应该使用any()::

("back" or "backwad" or "behind") in next6:

应该

any(direction in next6 for direction in ("back", "backwad", "behind")):
于 2012-02-07T20:07:22.180 回答