Python Forum
Login system not working - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/Forum-Python-Coding)
+--- Forum: General Coding Help (https://python-forum.io/Forum-General-Coding-Help)
+--- Thread: Login system not working (/Thread-Login-system-not-working)



Login system not working - Unknown_Relic - Nov-05-2019

im new to python and im trying to create a login system in python 3.7.3. There are two separate files called Usernames and Passwords. When i run the code it ruturns usernames not defined.
def Files():
    with open("Passwords.txt", "r") as f:
        passwords = [line.rstrip('\n') for line in f]
        f.close
    with open("Usernames.txt", "r") as f:
        usernames = [line.rstrip('\n') for line in f]
        f.close
        return passwords, usernames

def login(usernames, passwords):
    global enter_username
    enter_username = input("Input your username")
    if enter_username in usernames:
        enter_password = input("Input your password")
        if enter_password in passwords:
            username_pos = usernames.index(enter_username)
            password_pos = passwords.index(enter_password)
            if username_pos == password_pos:
                print("well done you logged in", enter_username)
                return True
            else:
                print("your password does not match the username")
        else:
            print("password not found")
    else:
        print("username is not authorised")
    return False
            

login(usernames, passwords)



RE: Login system not working - ThomasL - Nov-05-2019

You are not calling your function Files() (which is a VERY BAD function name!)
passwords, usernames = Files()
login(usernames, passwords)

You definitely should have a look here: PEP8 Python Style Guide


RE: Login system not working - buran - Nov-05-2019

also, as a side recommendations:
  • you don't need f.close() on line 4 and 7 - with context manager closes the file for you
  • you don't need line 11. enter_username is not global variable. And as a general rule you should avoid using global variables anyway.