2012-02-07 18 views
-1

Ok、raw_input()が正しく機能していませんか?

私は現在、単純なテキストのRPG(Pythonで)に取り組んでいます。しかし何らかの理由で、私の機能の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文は実行されません。次に、左、右、および前/後のコマンドだけが正しく機能します。私が入力したものは、 "front"/"forward"が実行するcrypt_room()関数を実行するだけです。

ありがとうございました。

+1

である必要があり、これは、Pythonでコーディングされていますか? – Bry6n

+0

@ Bry6nはい。私はそれについて言及することを完全に忘れていた。 – detroitwilly

+0

最初に定義されているグローバルが存在しないため、これを起動するのに問題が発生しています。したがって、ifの失敗です...しかし、私は動作し続けます。 – Bry6n

答えて

4

表現

"front" or "forward" in next6 

"front"に評価し、常にif文で真であると見なされます。おそらく何を意味するのですか

"front" in next6 or "forward" in next6 

このタイプのミスは、コード内に多くあります。 AtruthyBにそうでない場合、一般的に、発現

A or B 

Aに評価します。

補足として、プログラムのデザイン全体が壊れています。異なる部屋に入るときの再帰呼び出しは、最大再帰深度にすばやく到達します。

+0

+1これらのプログラミングミスや不一致などについての良い点は、読みやすさとコードの簡潔さの面で改善できる点もたくさんあります。 – Tadeck

+0

@タデック提案、みんなありがとう。 – detroitwilly

+0

また、私は再帰についてもっと読む必要があります。私は全体を実装するためのより良い方法を模索するつもりです。 – detroitwilly

0

Sven Marnachがあなたのコードがうまくいかない理由を述べました。それが正しい動作させるために、あなたはany() ::

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

を使用する必要があります

any(direction in next6 for direction in ("back", "backwad", "behind")): 
関連する問題