python – How to restrict user to only 3 attempts in guessing game – tkinter module

I am writing a program for a flag guessing game where the user sees an image of a flag and enters the name of the country it belongs to. I am able to get the output if the user enters the correct answer but I am not sure how to proceed with the part where the user enters the incorrect answer as I want the user to have only 3 attempts (which decrements and is displayed on the screen) after which he/she has to restart the game.

To achieve this I have written the function answer() where I have added a counter and assigned the value 3 to the variable guesses_remaining. Could someone advise if this is the right approach or if there is a better way to complete this task.

The entire code is as follows:

""" 
Import tkinter for developing the GUI. 
"""
from tkinter import *

# Initialization
root = Tk()

# Placing the GUI window on the top left corner of the user's computer
root.geometry('+0+0')
# Setting the title of the game
root.title("Flag Guessing Game")
# Setting the dimensions of the GUI window
root.geometry("300x400")

# Defining the heading of the game
lbl_heading = Label(root, text = "Welcome to this fun game!")
lbl_heading.pack(pady = 10)
lbl_heading.config(font=("Helvetica", 12, 'bold'))

# Inserting an image
image = PhotoImage(file="image2l.png")
setting = Label(root, image = image)
setting.pack()

# Define a new window when Rules is clicked
def create_window1():
    window1 = Toplevel(root)
    window1.title("Rules for this game")
    window1.geometry("600x150")
    tb1 = Label(window1, text = "The rules for this game are given below:")
    tb1.config(font=("Helvetica", 8, 'bold'))
    tb1.pack()
    tb2 = Label(window1, text = "1. Once you click on START, you will enter the game premises.")
    tb2.pack()
    tb3 = Label(window1, text = "2. Look at the image of the given flag.")
    tb3.pack()
    tb4 = Label(window1, text = "3. If you know which country the flag belongs to, please enter it in the text box provided.")
    tb4.pack()
    tb5 = Label(window1, text = "4. You will proceed to the next question only if you answer the current question correctly.")
    tb5.pack()
    tb6 = Label(window1, text = "5. If your answer is wrong, you will get threee chances after which you will need to restart the game.")
    tb6.pack()
    window1.mainloop()

# Create a button to read the rules of the game
B1 = Button(root, text="Rules", command=create_window1, width = 5)
B1.config(font=("Helvetica", 10, 'bold'))
B1.pack(pady = 15) 

# Define a new window when START is clicked
def create_window2():
    global e1
    global window2
    window2 = Toplevel(root)
    window2.title("Game Premises")
    window2.geometry("400x500")
    lbl_heading2 = Label(window2, text = "Guess which country this flag belongs to:")
    lbl_heading2.pack(pady = 10)
    lbl_heading2.config(font=("Helvetica", 12, 'bold'))
    
    # Insert flag image
    image2 = PhotoImage(file="USFlag.png")
    setting2 = Label(window2, image = image2)
    setting2.pack()

    # User input button
    Label(window2, text="Enter the name of the country").pack(pady = 10)
    e1 = Entry(window2)
    e1.pack(pady = 10)
    user_input = e1.get()

    # Create a submit button
    B2 = Button(window2, text="Submit", command=answer, width = 10)
    B2.config(font=("Helvetica", 10, 'bold'))
    B2.pack(pady = 15)

    quitb = Button(window2, text="Exit", command=window2.destroy, width = 5)
    quitb.config(font=("Helvetica", 10, 'bold'))
    quitb.pack(pady = 10)

    # Next button
    nextb = Button(window2, text="Next", command=create_window2, width = 5)
    nextb.config(font=("Helvetica", 10, 'bold'))
    nextb.pack()
    
    window2.mainloop()

def answer():
    user_input = e1.get()
    guesses_remaining = 3
    counter = 0
    if user_input == "USA":
        Label(window2, text="Correct answer!").pack(pady = 10)
    else:
        counter += 1
        guesses_remaining -= 1
        while counter != guesses_remaining:
            Label(window2, text="Incorrect answer. Please try again").pack(pady = 10)
            user_input = e1.get()
    
# Create a button to start the game
B3 = Button(root, text="START", command=create_window2, width = 10)
B3.config(font=("Helvetica", 14, 'bold'))
B3.pack()

# Exit button
exit_bmain = Button(root, text="Exit", command=root.destroy, width = 5)
exit_bmain.config(font=("Helvetica", 10, 'bold'))
exit_bmain.pack(pady = 10)

root.mainloop() 
```