# Separate the line into 2 parts, split at the delimiter. Also strips the \n from the end of the line def readAccount(line): return line.strip().split(":", 1) # Try to find the account in the file def findAccount(file, username): # Loop through each line ("account") in the file for line in file: # Separate the line into 2 parts, split at the ":" delimiter foundUsername, foundPassword = readAccount(line) # Comapre the usernames to see if they match if foundUsername == username: # If they match, return the username and password return [foundUsername, foundPassword] # If the account is not found, return None return None def promptAndFindAccount(): # Prompt the user to enter a username username = input("Enter a username: ") # Open the file in read mode to check if the username is already taken file = open("accounts.txt", "r") # Try to find the account account = findAccount(file, username) # We are done reading, close the file file.close() # Return the username and found account return username, account def createAccount(): # Try to find the account username, foundAccount = promptAndFindAccount() # If the account exists, print username already taken and return if foundAccount: print("Username already taken") return # The account doesn't already exist, so we can create it # Prompt the user to enter a password password = input("Enter a password: ") # Combine the username and password with a ":" delimiter account = username + ":" + password # Append the account to the file file = open("accounts.txt", "a") file.write(account + "\n") file.close() # Print success message print("Account created successfully") def login(): # Try to find the account username, existingAccount = promptAndFindAccount() # If the account doesn't exist, print login failed and try again if not existingAccount: print("Login failed. No account found.") return # Get the password from the account existingPassword = existingAccount[1] # If the account exists, compare passwords # Compare passwords password = input("Enter your password: ") # If the password is incorrect, print failed message and try again if password != existingPassword: print("Login failed. Incorrect password.") return # If the password is correct, print success message and return print("Login successful") def showAccounts(): # Open the file file = open("accounts.txt", "r") # Loop through each line and get the username and print it for line in file: username, _password = readAccount(line) print(f"Username: {username}") # We are done reading, close the file file.close() def main(): print("Welcome to the login system") while True: print ("--------------------------------") print("1. Create account") print("2. Login") print("3. Show accounts") print("4. Exit") # Prompt the user to enter a choice choice = input("Enter your choice: ") # If the choice is 1, create an account if choice == "1": createAccount() # If the choice is 2, login elif choice == "2": login() # If the choice is 3, show accounts elif choice == "3": showAccounts() # If the choice is 4, exit elif choice == "4": exit() # If the choice is invalid, print invalid choice and continue else: print("Invalid choice") main()