Python Forum

Full Version: Login system not working
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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)
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
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.