Python login system for a dice game This is a finished login system where 2 players have to enter their username and password to proceed onto the game. The usernames and passwords are stored in a text file in this formatusername: (username) password: (password). If one of the players enter their details in wrong it will loop it 3 times and if it is sill not correct they have to restart the code to try again.

import sys

counter = 0 
check_failed = True
while check_failed:
    print("Could player 1 enter their username and password")
    username1=input("Please enter your username ")
    password=input("Please enter your password ")
    with open("accountfile.txt","r") as username_finder:
        for line in username_finder:
            if ("username: " + username1 + " password: " + password) == line.strip():  
                print("you are logged in")
                check_failed = False
                check_failed = True
                while check_failed:
                    print("Could player 2 enter their username and password")
                    username2=input("Please enter your username ")
                    password=input("Please enter your password ")
                    with open("accountfile.txt","r") as username_finder:
                        for line in username_finder:
                            if ("username: " + username2 + " password: " + password) == line.strip():
                                print("you are logged in")
                                check_failed = False

                        else:
                            print("Sorry, this username or password does not exist please try again")
                            counter = counter + 1
                            if counter == 3:
                                print("----------------------------------------------------")
                                print("You have been locked out please restart to try again")
                                sys.exit()
        else:
            print("Sorry, this username or password does not exist please try again")
            counter = counter + 1
            if counter == 3:
                print("----------------------------------------------------")
                print("You have been locked out please restart to try again")
                sys.exit()


As I said this is a finished Login system and I just want suggestions on how I can improve this Login system.
One guiding principle in software development is Don't Repeat Yourself (DRY). Your code for player 1 and 2 are almost identical, so you should extract them into a login function:

import crypt

class AuthenticationError(Exception):
    pass

def encrypt(word, salt):
    # use some one-way hashing function here
    return crypt.crypt(word, salt)

def login(users, max_tries=3):
    for i in range(max_tries):
        username = input("Please enter your username ")
        password = input("Please enter your password ")
        if username in users and encrypt(password, username) == users[username]:
            print("You are logged in as", username)
            return username
        else:
            print(f"Login failed. You have {max_tries - i - 1} tries left.")
    raise AuthenticationError(f"You have been locked out please restart to try again.")


This uses a hashing function, like @ChadG suggested in their answer, with a salt to prevent rainbow table attacks.

By raising an Exception (here a custom one, to give it a nice name), the execution of the program is automatically halted, so no need to explicitly do sys.exit.

This code assumes that you have read the users file before into a dictionary (only need to do it once!), something like this:

import re

def get_users(file_name):
    line_fmt = re.compile(r'username: (.*) password: (.*)')
    with open(file_name) as f:
        return dict(line_fmt.match(line.strip()).groups() for line in f)


Or, this slightly expanded version which ensures that the entries not conforming to the format are skipped (including any trailing newline):

def get_users(file_name):
    line_fmt = re.compile(r'username: (.*) password: (.*)')
    users = {}
    with open(file_name) as f:
        for line in f:
            try:
                name, password = line_fmt.match(line.strip()).groups()
                users[name] = password
            except AttributeError:
                print("Malformed line:", line)
        return users


Use it like this:

if __name__ == "__main__":
    registered_users = get_users("accountfile.txt")

    print("Could player 1 enter their username and password")
    player1 = login(registered_users)

    print("Could player 2 enter their username and password")
    player2 = login(registered_users)

    # start dice game


This uses a if __name__ == "__main__": guard to allow importing from this script from another script.