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()
関数に返信します。
なぜ、あなたが 'path()'の中で 'confirmpath()'を定義するのですか?グローバルスコープで定義するだけです。 – chepner
はい、パス()関数の外で実行します – jonhid
再更新2: 'while confirmpaths'を' confirmpaths'に変更する必要があります –