2017-03-16 3 views
-2

私のコードを修正する方法がわかりません。 これは - > "NameError:囲みスコープ内の割り当ての前に参照される空変数 '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

ポスト*フルスタックトレース* –

+0

他のエラーがあまりにもあなたが正しい –

答えて

1

問題は、あなたがmainに対するローカル名としてaccesscontrolinfoを定義することです。したがってinfoaccesscontrolに呼び出すと、その名前は "所有されている"つまりmainのローカルなので、それを見つけることができません。代わりに、このような機能を有するの

def main(): 
    def accesscontrol(): 
     # ... 
    def info(): 
     # ... 
    # ... 
は次のように main()からそれらを移動

def accesscontrol(): 
    # ... 

def info(): 
    # ... 

def main(): 
    # ... 

をこのように、単にmain()を保つ:

def main(): 
    accesscontrol() 
    info() 
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() 
+0

@Nikhailこの問題ではありますが、それはOPがが持っていたものです。 – Morgoth

関連する問題