python - Global declaration error -


basically i'm creating program allows users create password , username. if done right program ask them log in. if password , username enter right stop , welcome.it works gives me error before program start , starts normally.

>>>  warning (from warnings module):   file "c:\python34\password.py", line 8     global username syntaxwarning: name 'username' assigned before global declaration >>>  warning (from warnings module):   file "c:\python34\password.py", line 29     global password syntaxwarning: name 'password' assigned before global declaration    import time  def usrname():     total=0     while total != 600:         time.sleep(1)         username=input("create username(must 6 chatacters long):")         global username          if len(username)== 6:              break         else:             time.sleep(1)             print("your username invalid")             print("try again")             total=total+1   def psw():     totalp=0     while totalp != 6:         time.sleep(1)          print("the password must 5-8 characters long")         print("contains atlease 1 capital letter")         password=input("please enter password:")         password2=input("please enter password again")         global password         if len(password)>5 , len(password)<8:             if password.title() == password:                 if password.title()==password2:                      break         else:             print("your password invalid, please try again")             totalp=totalp+1      option=input("do want create account(y/n)") if option.title()=="y":     usrname()     psw() else:     pass  print("hello please log in")  total3=0 while total3 < 12:     log1=input("what username?")     log2=input("what password?")     if log1 != username:          print("your  username invaild, please try again")         total3=total3+1      if log2 != password:         print("your password invaild, please try again")         total3=total3+1      else:         break if total3 > 12:     print("you must wait 2 days before enter username again")    else:     print("hello, wellcome program") 

there reason that. variables not initialised in scope want use them in. username , password variables initialized inside functions scope ends. code still working because have same name functions, when write

if log1 == username: 

it this:

if log1 == <function username @ 0x7f26d33dff28>: 

which reference function.

what need initialize variables in global scope before calling username , password methods. consider naming them else.

so this:

usrname="" pwd="" username() password() 

and change code these variables.

refer https://docs.python.org/2/reference/executionmodel.html scope information.


Comments