【问题标题】:Call a function inside a function? Help to fix [duplicate]在函数内部调用函数?帮助修复[重复]
【发布时间】:2017-08-07 07:47:48
【问题描述】:

我不确定如何修复我的代码,有人可以帮忙吗! 它打印出这个 --> “NameError: free variable 'info' referenced before assignment in enclosure scope”,我不知道如何使 info 成为全局变量,我认为这是问题所在......请帮忙!

import time
import random

admincode = ["26725","79124","18042","17340"]
stulogin = ["NikWad","StanBan","ChrPang","JaiPat","JusChan","AkibSidd","VijSam"]
teachercode = ["KGV"]

def main():
    def accesscontrol():
        global teachercode, stulogin, admincode
        print("Enter: Student,Teacher or Admin")
        option = input("--> ")
        if option == "Student":
            info()
        elif option == "Teacher":
            print("Enter you teacher code(xxx)")
            option = input
            if option == teachercode:
                  print("Access Granted")
                  info()
            else:
                 print("Please input the correct code!")
                 accesscontrol()
        elif option == "Admin":
            print("Enter your admin code(xxxxx)")
            option = input("--> ")
            if option == admincode:
                print("access granted, my master!")
        else:
            accesscontrol()
    accesscontrol()

    def info():
        print("Hello, enter your information below")
        usname = input("Username: ")
        pwname = input("Password: ")
        done = False
        while not done:
            print("Is this the information correct?[Y/N]")
            option = input("--> ")
            if option == "Y":
                print("Information saved")
                print("Username :",usname,"\nPassword:",pwname)
                done = True
            else:
                main()
        return info()
    info()

main()

【问题讨论】:

  • 发布完整的堆栈跟踪
  • 这个问题也有其他错误@Nikhail

标签: python


【解决方案1】:

问题是您将accesscontrolinfo 定义为相对于main 的本地名称。因此,当您在 accesscontrol 中调用 info 时,它找不到它,因为它是“拥有”的名称,换句话说,是 main 的本地名称。

而不是像这样的功能:

def main():
    def accesscontrol():
        # ...
    def info():
        # ...
    # ...

像这样将它们移出main()

def accesscontrol():
    # ...

def info():
    # ...

def main():
    # ...

因此保持main() 简单:

def main():
    accesscontrol()
    info()

【讨论】:

    【解决方案2】:

    您需要在调用之前定义info()。此外,您还给info() 打了一个不必要的电话,我已将其删除。

    import time
    import random
    
    admincode = ["26725", "79124", "18042", "17340"]
    stulogin = ["NikWad", "StanBan", "ChrPang", "JaiPat", "JusChan", "AkibSidd", "VijSam"]
    teachercode = ["KGV"]
    
    
    def main():
    
        def info():
            print("Hello, enter your information below")
            usname = input("Username: ")
            pwname = input("Password: ")
            done = False
            while not done:
                print("Is this the information correct?[Y/N]")
                option = input("--> ")
                if option == "Y":
                    print("Information saved")
                    print("Username :", usname, "\nPassword:", pwname)
                    done = True
                else:
                    main()
            return info()
    
        def accesscontrol():
            global teachercode, stulogin, admincode
            print("Enter: Student,Teacher or Admin")
            option = input("--> ")
            if option == "Student":
                info()
            elif option == "Teacher":
                print("Enter you teacher code(xxx)")
                option = input
                if option == teachercode:
                    print("Access Granted")
                    info()
                else:
                    print("Please input the correct code!")
                    accesscontrol()
            elif option == "Admin":
                print("Enter your admin code(xxxxx)")
                option = input("--> ")
                if option == admincode:
                    print("access granted, my master!")
            else:
                accesscontrol()
    
        accesscontrol()
    
    main()
    

    【讨论】:

    • 你是对的,这就是 OP 所拥有的。
    猜你喜欢
    • 2013-10-31
    • 2011-10-14
    • 2016-10-07
    • 1970-01-01
    • 2013-06-27
    • 1970-01-01
    • 2013-05-28
    • 1970-01-01
    • 2015-11-29
    相关资源
    最近更新 更多