0

下面的代码不断跳过“TryAgainQuestion()”函数:::: 我真的需要一些帮助!!!它变得烦人

    import time
    ranname = "0"

def AskForName():
    ranname = "1"
    name = input("Please enter your name: ")
    print("--------------------")
    print("  .:: ",name," ::.")
    print("--------------------")
    print("     ")
    time.sleep(2)
    Start()

def TryAgainQuestion():
    tryagain = input("Do you want to try again? (Y or N): ")
    if (tryagain == "y" or "Y"):
        AskForName()

    else:
        time.sleep(1)
        print("Made By: Daniel Parker")

def Start():
    if (ranname == "1"):
        TryAgainQuestion()

    if (ranname == "0"):
        AskForName()
Start()

谢谢,丹

4

2 回答 2

2

这是一个范围问题:ranmame第二行的与AskForName函数中的不一样。您需要global ranname在函数开头添加才能正确更新。

import time
ranname = "0"

def AskForName():
    global ranname
    ranname = "1"
于 2013-10-14T12:16:23.063 回答
0

正如@georgesl 所建议和所说的,这是范围问题(命名空间)

请阅读指南,我正在按类附加工作代码:

import time

class myClass():

    def __init__(self):
        self.ranname = "0"

    def AskForName(self):
        self.ranname = "1"
        name = raw_input("Please enter your name: ")
        print("--------------------")
        print("  .:: ", name, " ::.")
        print("--------------------")
        print("     ")
        time.sleep(2)
        self.Start()

    def TryAgainQuestion(self):
        tryagain = raw_input("Do you want to try again? (Y or N): ")
        if (tryagain == "y"):
            self.AskForName()

        else:
            time.sleep(1)
            print("Made By: Daniel Parker")

    def Start(self):
        if self.ranname == "1":
            self.TryAgainQuestion()

        if self.ranname == "0":
            self.AskForName()

def main():

    myclass = myClass()
    myclass.Start()

   #end of main

if __name__ == "__main__":
    main()
于 2013-10-14T12:23:26.173 回答