raw_input()无法正常工作?

时间:2012-02-07 15:01:25

标签: python raw-input

确定,

所以我目前正在研究一个简单的文本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()函数。

感谢。

2 个答案:

答案 0 :(得分:4)

表达式

"front" or "forward" in next6

评估为"front",并且在if语句中始终被视为true。您可能意味着什么

"front" in next6 or "forward" in next6

您的代码中存在此类错误。一般来说,表达式

A or B
如果Atruthy,则

评估为A,否则评估为B

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

答案 1 :(得分:0)

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

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

应该是

any(direction in next6 for direction in ("back", "backwad", "behind")):