I'm trying to write a register form for account creation. When using a while loop to check for username matches I notice the loop gets ran through multiple times therefore appending to the file multiple times. The file used is a simple text file containing a simple list of usernames and passwords. All I want is for the loop to check if the username input matches any username in the file, if so then it should prompt for a new username to be entered and then check if there is a match on that username. If there is no match then it should continue to let the user know the account was created and append the username and password to the end of the file.
################################### ########## Register File ########## ################################### # Open usrdata for read to check for username, pw match######## usrinfile = open("usrdata", 'r') usrdatalist = [] # Create a list to check for line in usrinfile: un, pw= (line.split(',')) usrdatalist.append([un, pw]) # Close file usrinfile.close() ############################################################### # Get information from user username = input("Enter a username: ") password = input("Enter a password: ") # Check if username already exists usroutfile = open("usrdata", 'a') for name in usrdatalist: while (name[0] == username): username = input("Name taken, enter a new name: ") if (name[0] != username): print("Account creation sucessful!") usroutfile.write(username + "," + password + '\n') usroutfile.close()