Home > Software design >  Username/Password Login System - Checking for Username correlation to password
Username/Password Login System - Checking for Username correlation to password

Time:09-23

currently I'm trying to have my program check to make sure that the username that was entered in UserInfo.txt, and password that was entered in PassInfo.txt are located in the same line. For example: The system will check to see username1 (which is on line 5) and password1 (which is also on line 5) are on the same lines and are correctly tide together in order to login. Currently right now I have it as it just takes the first username that is entered and checks the file to find the first username1 in the file and uses that. Same thing with the password verifcation. Any help would be much appreciated.

Here's the code:

def loginChecker():
userLog = userLogin.get()
passLog = passLogin.get()

userLogin.delete(0,END)
passLogin.delete(0,END)

#Username Checker
flagU = 0
indexU = 0
userfile=open("UserInfo.txt", "r")
for line in userfile:
    indexU  = 1
    if userLog in line:
        flagU = 1
        break
#Password Checker
flagP = 0
indexP = 0
passfile=open("PassInfo.txt", "r")
for line in passfile:
    indexP  = 1
    if passLog in line:
        flagP = 1
        break
#Responses for specific Scenarios
if flagP == 1 and flagU == 1:
    messagebox.showinfo("Success", "You have been successfully logged in")
    desScreen3()
    desScreen1()
elif flagP == 1 and flagU == 0:
    messagebox.showerror("Error", "Password is incorrect")
else:
    messagebox.showerror("Error", "Username is incorrect")

CodePudding user response:

Read one line from each file and compare them to the user-entered information. Keep going until they match, or you run out of lines.

success = False
try:
    with open("usernames.txt") as usernames, open("passwords.txt") as passwords:
        while True:
            name = next(usernames).rstrip()
            pw = next(passwords).rstrip()

            if name == input_name and pw == input_pw:
                success = True
                break
except StopIteration:
    # reached end-of-file without finding a match
    pass

if success:
    print("found a match")
else:
    print("did not find a match")
  • Related