2016-08-24 14 views
-11

私は、一例として、これを書いた:Python関数の関数エラー?

def function(): 
choice = raw_input("> ") 
if choice == "attack": 
    print "Killed the monster." 
elif choice == "run": 
    print "You run but the monster follows you. Activate jetpack? (y/n)" 
    def function_in_function(): 
     choice = raw_input("> ") 
     if choice == "y": 
      print "You fly to safety." 
     elif choice == "n": 
      print "You get stomped." 
     else: 
      print "That's not an option." 
      function_in_function() 
else: 
    print "That's not an option." 
    function() 

私はパワーシェル上でそれを開いたが、何も来ませんか?

+1

トップラインより下のものは、少なくとも4スペース分インデントする必要があります。 – Andrew

+1

インデントが間違っています。 –

+3

これが.pyファイルとして保存され、そのファイルを実行すると、関数を定義して実行することはないので何も起こりません。 – syntonym

答えて

0

これはあなたの希望する機能とフォーマットですね。これは必須ではないのでここでは関数内の関数の使用をお勧めしませんが、それはまだ示されているように行うことができます。インデントについて覚えておいてください。ここをクリックしてhttp://www.python-course.eu/python3_blocks.phpを読んでください。また、関数を代入する前に関数を呼び出すことはできません。

def function(): 

    choice = raw_input("> ") 
    if choice == "attack": 
     print "Killed the monster." 
    elif choice == "run": 
     print "You run but the monster follows you. Activate jetpack? (y/n)" 
     def function_in_function(): 
      choice = raw_input("> ") 
      if choice == "y": 
       print "You fly to safety." 
      elif choice == "n": 
       print "You get stomped." 
      else: 
       print "That's not an option." 
       function_in_function() 
     function_in_function() 
    else: 
     print "That's not an option." 
     function() 


function() 

編集:コメントにしたがってください。

+0

ありがとうございましたが、私の主なポイントは、 "それはオプションではありません"というときでした。スクリプト全体を終了するのではなく、「選択肢」に戻るようにしたいと思っています:) – Keretto

+0

"function()"私の答えにそれを加えました。 – grael

+0

私は今それを得るが、私が書いたものよりも複雑に見える。私がその順序でそれをしたら、それはまだ動くだろうか? – Keretto