如何在Python 2.7中全局访问嵌套函数

时间:2016-03-21 17:38:56

标签: python function nested

我正在尝试找到一种方法来访问confirmpath()函数中的pathcond()函数。请帮忙。

PS。我刚刚开始学习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()

更新 我在该程序上进一步工作了一段时间,最终得到了一个现在没有问题的版本。我在下面发布了它,请通过建议我可以更改/改进/删除来帮助我做得更好。代码如下:

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()

更新2:代码正常运行,但最终仍会多次打印以下字符串

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

我观察到它会在回复confirmpath()函数时多次打印字符串,无论我的响应是什么。

2 个答案:

答案 0 :(得分: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()

说明:的 首先,将函数放在全局范围内,因为在这种情况下,如果要在不同的地方调用内部函数,那么执行您正在执行的操作(在函数中定义函数)是没有意义的。其次,不是使用全局变量,而是将变量传递给函数。使用函数使用的全局变量不是好的做法,并导致意大利面条代码和阴影变量Why are global variables evil?。使用函数的参数将值传递给它。

我测试了上面的代码,它有效。

答案 1 :(得分: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()

调用Game()时,会创建一个对象(实例)。该类中的所有实例方法都引用self,它是此实例的名称。

虽然这看起来有点多,但课程可能非常有用,并且是一个熟悉的好概念。