2017-01-07 3 views
-2

Pythonの関数を使用すると、多くの問題が発生しています。私は初心者ですので、私はこのような迷路をゲームのように作り出しています。そして、ユーザーが以前の場所に戻ることができるように関数を使いたいと思っています。問題は、 "def sec2():"の後のすべてのコードが実行されず、このコードブロックを完全にスキップすることです。したがって、ユーザーがプログラムを実行して左を選択する場合、プログラムは「ああ、気にしないで、それらの番号を覚えている」ように終了し、ユーザーに何かを促したり、sec2から何も印刷したりしません。私は私の問題がインデントで起こっていると信じています。私の機能の下にあるコードが実行されていない理由を誰かが知っているなら、私に知らせてください!どうもありがとう!Pythonで関数を定義する - コードブロックが実行されない

print ("********") 
print ("Hey there soldier. This is General Chris.") 
print ("I understand you are in quite the situation!") 
print ("Just 4 hours ago, your patrol was ambushed by ISIS...You may not rememeber much after being knocked unconscious!") 
name = input('Whats your name, soldier?') 
print ("********") 
print ("Alright, here's the deal",name) 
print ("You are now being held hostage in an encampment near Soran, Iraq.") 
print ("Unfortunately for you, our hackers have recieved intel that your captors plan on executing you in just two hours.") 
print ("You dont have long to make your escape! Get moving fast!") 

def sec1(): 
    print ("********") 
    print ("You have two doors in front of you. Do you choose the door on the left or right?") 
    room1 = input('Type L or R and hit Enter.') 

    if room1 == "L": 
     print ("********") 
     print ("Good choice",name) 
     print ("Whats this? A slip of paper says '8642' on it...Could it mean something?") 
     print ("Ah, nevermind! Remember those numbers!") 

     def sec2(): 
      print ("********") 
      print ("Now you have a choice between crawling into the cieling, or using the door!") 
      room2 = input('Type C for cieling or D for door, and hit Enter!') 

      if room2 == "C": 
       print ("********") 
       print ("Sure is dark up here in the ducts!") 
       print ("Stay quiet, and move very slowly.") 

       def ductoptionroom(): 
        print ("Do you want to continue into the ducts or retreat?") 
        ductoption = input('Type C or R and hit Enter!') 

        if ductoption == "C": 
         print ("You are continuing into the ducts!") 

        elif ductoption == "R": 
         sec2() 

        else: 
         print ("Focus on the mission!") 



      elif room2 == "D": 
       print ("********") 
       print ("You slowly turn the door knob and see a guard standing there...He doesnt notice you!") 
       print ("Look, theres a piece of rope on the ground! You could use this to strangle the guard!") 

       def guardkillroom(): 
        print ("Do you want to try and kill the guard so you can continue on your escape?") 
        guardkill = input ('Type K for kill or R for retreat and hit Enter!') 

        if guardkill == "K": 
         print ("********") 
         print ("You sneak behind the unsuspecting guard and quickly pull the rope over his neck!") 
         print ("You've strangled the guard! Now you can continue on your mission!") 

        elif guardkill == "R": 
         guardkillroom() 

        else: 
         print ("We dont have all day!") 
         guardkillroom() 

      else: 
       print ("Focus soldier!") 
       room2() 



    elif room1 == "R": 
     print ("********") 
     print ("Uh oh. Two guards are in this room. This seems dangerous.") 
     print ("Do you want to retreat or coninue?") 
     roomr = input('Type R or C and hit enter.') 

     if roomr == "R": 
      print ("********") 
      print ("Good choice!") 
      sec1() 

     elif roomr == "C": 
      print ("********") 
      print ("You continue and are spotted by a guard.") 
      print ("***MIISSION FAILED***") 

     def gameover1(): 
      print ("Do you want to retry?") 
      retry1 = input("Type Y or N and hit enter!") 

      if retry1 == "Y": 
       sec1() 

      elif retry1 == "N": 
       print ("********") 
       print ("Thanks for playing!") 

      else: 
       gameover1() 
sec1() 
+1

なぜsec2を 'sec1'の中に定義しますか?すべての機能をモジュールレベルに保ってください。 – Matthias

+0

[Pythonで関数の名前を持つ文字列からモジュールの関数を呼び出す]の可能な複製(https://stackoverflow.com/questions/3061/calling-a-function-of-a-module-from-a-関数名でのPythonの文字列) – Chris

答えて

1

sec2と定義しているに過ぎず、実際には決して呼び出されていないようです。 def myfunc()のようなものを実行すると、その関数が呼び出されたときに、になるはずです。コード内の他の場所からmyfunc()を実行するまで、コードは実際には実行されません。だから私はどこそれがすべきか分からないsec1 のどこか別の場所からそれを呼び出す必要がありsec2を使用する

を(プレイヤーがダクトから撤退することを決定した場合)と呼ばれている唯一の場所sec2sec2内から再帰的ですゲームの迅速な読み取りに基づくものではなくテストするために、あなたはsec2のみ、これまでにもsec1の内部で呼び出すことができることを意味しsec1の内部で定義されているsec2以来、さらに次

print ("Ah, nevermind! Remember those numbers!") 

    def sec2(): 
     .... 


    elif room2 == "D": 
     sec2() 

ような何かを行うことができます。私はそれが意図されたものではないと思う(間違っているかもしれないが)。これを修正するには、以下を実行することができます。

def sec2(): 
    ... 
def sec1(): 
    ... #All the same code as before except for the sec2 definition 

sec1() 
関連する問題