A game of Mastermind for python [closed]

I have used bits and pieces of code to come up with this. My problem is that I cant get to print the correct numbers in the correct place for every guess taken. The second response, Correct number but not in place is working perfect. I don’t really know what I am missing.

import random

def run_game():
    four_digit_code = []
    
    while len(four_digit_code) < 4:
        random_code = random.randint(1, 8)
        
        if random_code not in four_digit_code:
            four_digit_code.append(random_code)
            four_digit_code_string = str(four_digit_code)
            four_digit_code_list = list(four_digit_code)
            
            # Testing
            #print(four_digit_code)
            #print(four_digit_code_list)
            
    number_of_turns = 12
    more_turns_left = True

    while more_turns_left:
        four_digit_code = "".join(map(str, four_digit_code))
        user_four_digit_code = input("Input 4 digit code: ").strip()

        if len(user_four_digit_code) != 4 or not (user_four_digit_code):
            print("Please enter exactly four digits")
            continue

        elif user_four_digit_code == four_digit_code_string:
            print("Number of correct digits in correct place: {}".format(len(user_four_digit_code)))
            print("Number of correct digits not in correct place: {}".format(0))
            print("Congratulations! You are a codebreaker!")
            print("The code was: {}".format(four_digit_code))
            break

        else:
            digits_in_correct_place = 0
            digits_in_wrong_place = 0

            for index in range(len(four_digit_code_list)):
                if user_four_digit_code[index] == four_digit_code_list[index]:
                    digits_in_correct_place += 1

                elif user_four_digit_code[index] in four_digit_code_string:
                    digits_in_wrong_place += 1

        print("Number of correct digits in correct place: {}".format(digits_in_correct_place))
        print("Number of correct digits not in correct place: {}".format(digits_in_wrong_place))
        number_of_turns -= 1
        print("Turns left: {}".format(number_of_turns))

        if number_of_turns == 0:
            print("The code was: {}".format(four_digit_code_string))
            more_turns_left = False

if __name__ == "__main__":
    run_game()

Answer

four_digit_code_string is wrong, that’s not how you build a string from a list. try this instead:

four_digit_code_string=''.join([str(x) for x in four_digit_code])