-1

Ok、

だから私は現在 (Python で) シンプルなテキスト RPG に取り組んでいます。しかし、何らかの理由で、私の関数の 1 つが入力を奇妙に読み取っています。

現在、ダンジョン内の各部屋は個別の機能です。動かない部屋はこちら

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 ステートメントは実行されません。その上で、左、右、および前後のコマンドのみが正しく機能します。私が入力した他のものは、「フロント」/「フォワード」が実行する必要があるcrypt_room()関数のみを実行します。

ありがとう。

4

2 に答える 2

4

表現

"front" or "forward" in next6

ステートメントで評価され"front"、常に真であると見なされます。ifあなたがおそらく意味するのは

"front" in next6 or "forward" in next6

コードには、このタイプの間違いがさらにあります。一般的に、式

A or B

真実であるかどうかを評価し、そうでないA場合Aは評価します。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 に答える