我不确定如何修复我的代码,有人可以帮忙! 它打印这个 - > “NameError:在封闭范围中赋值之前引用的自由变量'info',我不知道如何使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()
答案 0 :(得分:1)
问题是您将accesscontrol
和info
定义为相对于main
的本地名称。因此,当您在info
内拨打accesscontrol
时,它无法找到它,因为它是一个名称"拥有"换句话说就是main
的本地。
而不是像这样的功能:
def main():
def accesscontrol():
# ...
def info():
# ...
# ...
将它们移出main()
,如下所示:
def accesscontrol():
# ...
def info():
# ...
def main():
# ...
因此简单地保持main()
:
def main():
accesscontrol()
info()
答案 1 :(得分:0)
您需要在调用之前定义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()