I have a program where I ask the user their username and password, and it checks a file to see if they have the correct username and password. Each username and password is stored on its own line.
this is what I have
if username and password in open("logininfo").read(): print("logged in succesfully") else: print("Incorrect username or password")
the problem i'm having with this is that any username can be used with any password since it check the whole file. Is there any way to check if they're on the same line in the file?
You can easily make this function checking the file line by line
def check_password(username, password, lines): for line in lines: if username and password in line: return True return False
And you can use this function this way:
check_password(username, password, open(file_name).readlines())
open('logininfo').readlines() and check whether the username is in a line AND the password is in a line.
if username and password in open("logininfo"), it checks whether string
username is not empty or
None, which is not intended, so you need to check both the username and the password separately.