Python login program
Question: I'm writing a login program in Python,it takes the usernames and the
passwords,and stores it for each user in a separate .txt file. I can already
register,the program creates the file,but i can't login. Here's the code:

    
    
    ###############
    import getpass
    import time
    ussr=False
    passwd1=False
    acc=False
    notin=False
    reg=False
    t=[]
    ###############
    #f;a;;b;c;d;z;;;;;
    ###############
    def bor():
        global acc
        print("1-->Login")
        print("2-->Register")
        a=int(input("B/R: "))
        if  a==1:
            acc=True
        if a==2:
            acc=False
    
    def ACCOUNT():
        if acc==True:
            login()
        if acc==False:
            register()
    
    def file():
        global jel
        f=open(z,"r")
        sor=f.read()
        jel=sor.strip().split()
        for s in jel:
            t.append(s)
        f.close()
    
    
    def register():
        global reg
        reg=True
        global z
        b=input("Username: ")
        c=getpass.getpass('Passwd')
        z=input("Filename(.txt!):")
        f2=open(z,"w")
        f2.close()
        f=open(z,"r+")
        if b not in f:
            notin=True
        f.close
        if notin==True:
            f1=open(z,"a")
            f1.write(b)
            f1.write(c)
            f1.close
        if notin==False:
            print("This username is already taken")
            exit
    
    def login():
        global usr
        global passwd
        global passwd1
        global ussr
        global z
        usr=input("Username: ")
        passwd=getpass.getpass('Password')
        z=input("Filename(.txt!):")
        for i in t:
            if usr==i:
                ussr=True
            if passwd==i:
                passwd1=True
    
    def check():
        if reg==True:
            exit
        if ussr==True and passwd1==True:
            print("Login succesful")
            time.sleep(12)
        if ussr==True and passwd1==False:
            print("Wrong password")
        if ussr==False and passwd1==True:
            print("Wrong username")
    
    
    
    
    
    bor()
    ACCOUNT()
    file()
    check()
    


Answer: Please avoid using global variables, and start programming more "pythonic".
Begin using a class object and function arguments. Really, global variables
are BAD.

Then:

    
    
    def ACCOUNT():
        if acc==True:
            login()
        if acc==False:
            register()
    

Is an horrible statement.

    
    
    def ACCOUNT(acc):
       if acc: login() 
       else: register()
    

is way better.

You have to consider "if, else, elif", when you write conditions:

    
    
    if a == 1: 
       doSomething()
    elif a == 2:
       doSomethingElse()
    else:
       doAnotherThing()
    

To be clear, unless you need to check the variable type, generally "if
varname" is ok (it will return false if an object is empty, or if a boolean
value is equal to False and None as well).

If you have to check on a boolean value, you generally don't have to specify
the condition you're looking for.

"if not a" is _more pythonic_ than "if a == False" (but, again, it will match
"None" as well and not just "False"!!)

And tell yourself, everytime you declare a global variable: "**I am doing
something wrong, and there's for sure a better way to do this**.

You can pass variables as function arguments:

    
    
    def sumFunction(arg1, arg2):
        return arg1 + arg2
    

and you can declare classes, to store variables, functions that work on the
same task, and start programming better:

    
    
    class Authentication():
        def __init__(self):
            # the __init__ class method is run everytime the function is istantiated
            self.example = 'I am an example'
    
        def Login(self, username, password):
            # this will do authentication things with
            # username and password variables, that lives
            # ONLY in this function (namespace)
    
        def Register(self, username, password):
            # do register things
    

you can then istantiate the class Authentication like this

    
    
    auth = Authentication()
    

and use the instance as you wish:

    
    
    auth.Login(username, password)
    auth.Register(username, password)
    

"self" is the class namespace, viewed from itself. You can store whatever you
like in it, and you can call sub-functions in class from one to another,
prepending the "self." prefix

In the example I have provided, you can access the "I am an example text" in
this way:

    
    
    >>> x = Authentication()
    >>> x.example
    'I am an example'
    

Finally, when you declare a class method (a function in a class), you always
have to specify "self" at first argument, but you don't need to pass it when
calling the method from the outside (so a class method with 3 arguments:
(self, arg1, arg2), will expect TWO arguments to work, just arg1 and arg2.

**Read more, write less** , good luck.

