import getpass
credentials = {} ## Sets up an array for the login credentials
with open('Usernames.txt', 'r') as f: ## Opens the file and reads it
for line in f: ## For each line
username, delim, password = line.strip().partition(':') ## Separate each line into username and password, splitting after a colon
credentials[username] = password.split(';') ## Links username to password
while True:
username = raw_input("Please enter your username: ") ## Asks for username
if username in credentials: ## If the username is in the credentials array
while True:
password = getpass.getpass("Please enter your password: ") ## Asks for password
if password == credentials[username][0]:
print "Logged in successfully as " + username ## Log in
break
elif password in credentials[username]: ## If the password is linked to the username
print "Specific error message " + username ## Log in
else:
print "Password incorrect!"
break
else:
print "Username incorrect!"
It's simpler though if you just ask for a username/password fresh each time. The way you have it - if the user enters someone else's username by mistake they are stuck in a loop forever unless they can guess the password.
while True:
username = raw_input("Please enter your username: ")
password = getpass.getpass("Please enter your password: ")
if username in credentials and password == credentials[username][0]:
print "Logged in successfully as " + username
break
elif password in credentials.get(username, []):
print "Specific error message " + username
else:
print "Login incorrect!"