-2

I'm not sure how to fix my code, could someone help! It prints this --> "NameError: free variable 'info' referenced before assignment in enclosing scope", I don't know how to make info a global variable, i think that is the problem...Someone please help!

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

2 Answers2

1

The problem is that you define accesscontrol and info as local names relative to main. So when you call info inside accesscontrol it can't find it, because it's a name "owned" by in other words local to main.

Instead of having the functions like this:

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

Move them out of main() like this:

def accesscontrol():
    # ...

def info():
    # ...

def main():
    # ...

Thus keeping main() as simply:

def main():
    accesscontrol()
    info()
vallentin
  • 23,478
  • 6
  • 59
  • 81
0

You need to define info() before it is called. Also you had an unnecessary call to info(), which I removed.

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()
Morgoth
  • 4,935
  • 8
  • 40
  • 66