mardi 30 décembre 2014

How to check the second list element on a line in a file contains 'x'?

What I'm trying to do is to create a login system.

I ask the user for a username, and if that username is in the file, I want to ask for a password.

But I don't know how I could check if the username and password matches.


The file with usernames is formated like this:



Username1 Password
Username2 Password


My code is:



def login():
users = open('passwords.txt', 'r').readlines()
while x == 1:
usernames = [i.split()[0] for i in users]
username = raw_input("> ")
if username.lower() in usernames:
password = raw_input("> ")


else:
print 'That username is not in use.'


So the question is, how may I check if I.E: Password matches with Username1?

I know I can't loop through, every password in the file like this:



passwords = [i.split()[1] for i in users]


Because, then it would accept every single password in the file, and not the matching one only.


Aucun commentaire:

Enregistrer un commentaire