Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Login system not working
#1
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)
Reply
#2
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
Reply
#3
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.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  in a login interface when i try login with a user supposed to say test123 but nothing NullAdmin 3 2,218 Feb-20-2021, 04:43 AM
Last Post: bowlofred
  Difference between os.system("clear") and os.system("cls") chmsrohit 7 16,495 Jan-11-2021, 06:30 PM
Last Post: ykumar34
  Login and Register system finndude 1 2,355 Apr-24-2020, 10:05 PM
Last Post: deanhystad
Question Difference between Python's os.system and Perl's system command Agile741 13 6,660 Dec-02-2019, 04:41 PM
Last Post: Agile741
  Login System Carbonix 4 4,386 Feb-04-2019, 02:19 PM
Last Post: Larz60+
  Python login system help. calumw20 1 3,646 Mar-06-2018, 08:01 AM
Last Post: buran
  Creating a Login System Using Python- HELP tesilstudent112 0 9,527 Dec-08-2017, 07:17 PM
Last Post: tesilstudent112
  Login System ItsBlueey 1 36,988 Nov-06-2017, 05:24 PM
Last Post: heiner55

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020