2016-03-21 22 views
0

pathcond()関数のconfirmpath()関数にアクセスする方法を探しています。助けてください。Python 2.7でネストされた関数にグローバルにアクセスする方法

PS。私はちょうど約1週間前にPythonを学び始めました。コードの簡潔さと私の全体的なスキルの向上については、本当に感謝しています。

def name(): 
    global call 
    call = raw_input("What is your name?\n") 
    print("Hello " + call) 

def game(): 
    global charchoose 
    charchoose = raw_input("What will be your character " + call + ": Mage, Wizard or Knight?\n") 
    print("You chose " + charchoose) 

def path(): 
    pathchoose = raw_input("You are a " + charchoose + " who was walking down Tranversia and came across a three-way road. Which on will you choose? Land, Sea or Clouds\n").lower() 
    def confirmpath(): 
     global confirmpath 
     confirmpath = raw_input("You chose " + pathchoose + ". Are you sure you want to continue? Yes or No?\n").lower() 
     pathcond() 
    confirmpath() 

def pathcond(): 
    while confirmpath == "no": 
     path() 
    if confirmpath == "yes": 
     print("Good choice, you win!") 
    else: 
     print("Sorry, we didn't get that. Can you answer again, please?") 
     confirmpath() 

def ask(): 
    askplay = raw_input("Would you like to play a game? Yes or No?\n").lower() 
    if askplay == "yes": 
     game() 
     path() 
    elif askplay == "no": 
     print("That's alright. Thanks for hanging out, though. Bye!") 
    else: 
     print("Sorry, I didn't get that. Please try again.") 
     ask() 

name() 
ask() 

UPDATE: 私はいつかのためのプログラムにさらに協力し、今問題に無料でバージョンになってしまった後

は私が助けを必要とするコードです。私はそれを下に掲載しました。私が改善/改善/改善できるものを提案することによって、より良いものになるのを助けてください。コードは以下の通りである:

def Initiate(): 
    global call 
    call = raw_input("What is your name?\n") 
    print("Hello " + call) 
    begin() 

def game(): 
    global charchoose 
    charchoose = raw_input("What will be your character " + call + ": Mage, Wizard or Knight?\n") 
    print("You chose " + charchoose) 
    path() 

def path(): 
    global pathchoose 
    pathchoose = raw_input("You are a " + charchoose + " who was walking down Tranversia and came across a three-way road. Which on will you choose? Land, Sea or Clouds\n").lower() 
    confirmpath() 

def confirmpath(): 
    global confirmpaths 
    confirmpaths = raw_input("You chose " + pathchoose + ". Are you sure you want to continue? Yes or No?\n").lower() 
    pathcond() 

def pathcond(): 
    while confirmpaths == "no": 
     path() 
    if confirmpaths == "yes": 
     print("Good choice, you win!") 
    else: 
     print("Sorry, we didn't get that. Can you answer again, please?") 
     confirmpath() 


def begin(): 
    askplay = raw_input("Would you like to play a game? Yes or No?\n").lower() 
    if askplay == "yes": 
     game() 
    elif askplay == "no": 
     print("That's alright. Thanks for hanging out, though. Bye!") 
    else: 
     print("Sorry, I didn't get that. Please try again.") 
     ask() 

Initiate() 

UPDATE 2:コードが正常に機能するが、それでも何度

if confirmpaths == "yes": 
    print("Good choice, you win!") 

次の文字列を印刷して終了、私はそれが何回として文字列を出力することが観察され私の応答が何であっても、confirmpath()関数に返信します。

+3

なぜ、あなたが 'path()'の中で 'confirmpath()'を定義するのですか?グローバルスコープで定義するだけです。 – chepner

+0

はい、パス()関数の外で実行します – jonhid

+0

再更新2: 'while confirmpaths'を' confirmpaths'に変更する必要があります –

答えて

1

本質的には、Finite State Machineを作成しようとしています。 Pythonでは、これはおそらくクラスを使って実装するのが最適です。役に立つかもしれないsimilar exampleがあります。

クラスに関する重要な部分は、ゲーム内のデータを追跡でき、グローバル変数を避けることができることです。ここであなたのプログラムはクラスに変換されます。

class Game: 
    def __init__(self): 
     self.name = raw_input("What is your name?\n") 
     print("Hello " + self.name) 
     self.ask() 

    def ask(self): 
     askplay = raw_input("Would you like to play a game? Yes or No?\n").lower() 
     if askplay == "yes": 
      self.game() 
     elif askplay == "no": 
      print("That's alright. Thanks for hanging out, though. Bye!") 
     else: 
      print("Sorry, I didn't get that. Please try again.") 
      self.ask() 

    def game(self): 
     self.charchoose = raw_input("What will be your character " + self.name + ": " 
      "Mage, Wizard or Knight?\n") 
     print("You chose " + self.charchoose) 
     self.path() 

    def path(self): 
     pathchoose = raw_input("You are a " + self.charchoose + " " 
      "who was walking down Tranversia and came across a three-way road. " 
      "Which on will you choose? Land, Sea or Clouds\n").lower() 
     self.confirmpath(pathchoose) 

    def confirmpath(self, pathchoose): 
     confirmpaths = raw_input("You chose " + pathchoose + ". " 
      "Are you sure you want to continue? Yes or No?\n").lower() 
     self.pathcond(confirmpaths, pathchoose) 

    def pathcond(self, confirmpaths, pathchoose): 
     if confirmpaths == "no": 
      self.path() 
     if confirmpaths == "yes": 
      print("Good choice, you win!") 
     else: 
      print("Sorry, we didn't get that. Can you answer again, please?") 
      self.confirmpath(pathchoose) 

Game() 

が呼び出されると、オブジェクト(インスタンス)が作成されます。クラス内のすべてのインスタンスメソッドは、このインスタンスの名前であるselfを参照しています。

これは少しのように見えるかもしれませんが、クラスは非常に便利でよく慣れるための良い概念です。

1

ここにいくつかの問題がありますが、あなたのために修正します。

def name(): 
    global call 
    call = raw_input("What is your name?\n") 
    print("Hello " + call) 

def game(): 
    global charchoose 
    charchoose = raw_input("What will be your character " + call + ": Mage, Wizard or Knight?\n") 
    print("You chose " + charchoose) 

def path(): 
    pathchoose = raw_input("You are a " + charchoose + " who was walking down Tranversia and came across a three-way road. Which on will you choose? Land, Sea or Clouds\n").lower() 
    confirmpath(pathchoose) 

def confirmpath(pathchoose): 
    confirmpath_var = raw_input("You chose " + pathchoose + ". Are you sure you want to continue? Yes or No?\n").lower() 
    pathcond(confirmpath_var) 

def pathcond(confirmpath_var): 
    while confirmpath_var == "no": 
     path() 
    if confirmpath_var == "yes": 
     print("Good choice, you win!") 
    else: 
     print("Sorry, we didn't get that. Can you answer again, please?") 
     confirmpath() 

def ask(): 
    askplay = raw_input("Would you like to play a game? Yes or No?\n").lower() 
    if askplay == "yes": 
     game() 
     path() 
    elif askplay == "no": 
     print("That's alright. Thanks for hanging out, though. Bye!") 
    else: 
     print("Sorry, I didn't get that. Please try again.") 
     ask() 

name() 
ask() 

説明: まず第一に、あなたがしている場合は、この文脈では、それはあなたが(関数内で関数を定義する)やっているやっても意味がないので、単にグローバルスコープにあなたの機能を置きます他の場所でその内部関数を呼び出す。第2に、グローバル変数を使用する代わりに、変数を関数に渡します。関数が使用するグローバル変数を使用するのは良い方法ではなく、スパゲッティコードとシャドー変数Why are global variables evil?につながります。関数のパラメータを使用して値を渡します。

私は上記のコードをテストしました。

+0

else条件を実行するための状況を意図的に作成しようとすると、 pathcond(confirmpath_var)関数。 –

関連する問題