Python tkinter quiz app, correct answers shown as wrong

I’m kind of a beginner in python and tkinter. I made a basic quiz app for a project in college, which displays random 10 questions out of a dict of 30 questions.

In the QUESTIONS dictionary, each question has 4 answers with the first being the correct answer. In the generate_quiz() function, each question has a correct answer shuffled amongst 3 others. At the end of the quiz, it resets everything when going back to the main menu. When selecting correct answers in the 1st round the quiz goes smoothly, but when I restart the quiz and choose the same correct answers it displays them as wrong. This only happens for the questions that are in common with the ones from the previous round, the new questions have no such issue.

Any help would be greatly appreciated!

Here’s the entire code:

from tkinter import *
import random

QUESTIONS = {
    "What is the capital of France?": ["Paris", "London", "Berlin", "Madrid"],
    "Who wrote 'To Kill a Mockingbird'?": ["Harper Lee", "Mark Twain", "Jane Austen", "J.K. Rowling"],
    "What is the largest planet in our solar system?": ["Jupiter", "Mars", "Earth", "Venus"],
    "What is the chemical symbol for water?": ["H2O", "CO2", "O2", "NaCl"],
    "Who was the first president of the United States?": ["George Washington", "Thomas Jefferson", "Abraham Lincoln", "John Adams"],
    "What is the square root of 64?": ["8", "6", "7", "9"],
    "Who painted the Mona Lisa?": ["Leonardo da Vinci", "Vincent van Gogh", "Pablo Picasso", "Claude Monet"],
    "What is the longest river in the world?": ["Nile", "Amazon", "Yangtze", "Mississippi"],
    "What is the smallest country in the world?": ["Vatican City", "Monaco", "Nauru", "San Marino"],
    "What is the fastest land animal?": ["Cheetah", "Lion", "Tiger", "Leopard"],
    "Who discovered penicillin?": ["Alexander Fleming", "Marie Curie", "Louis Pasteur", "Isaac Newton"],
    "What is the hardest natural substance on Earth?": ["Diamond", "Gold", "Iron", "Silver"],
    "What is the capital of Japan?": ["Tokyo", "Beijing", "Seoul", "Bangkok"],
    "Who wrote '1984'?": ["George Orwell", "Aldous Huxley", "Ray Bradbury", "J.R.R. Tolkien"],
    "What is the chemical symbol for gold?": ["Au", "Ag", "Pb", "Fe"],
    "What is the tallest mountain in the world?": ["Mount Everest", "K2", "Kangchenjunga", "Lhotse"],
    "What is the main ingredient in guacamole?": ["Avocado", "Tomato", "Onion", "Garlic"],
    "Who is known as the father of computers?": ["Charles Babbage", "Alan Turing", "John von Neumann", "Bill Gates"],
    "What is the largest ocean on Earth?": ["Pacific Ocean", "Atlantic Ocean", "Indian Ocean", "Arctic Ocean"],
    "What is the speed of light?": ["299,792,458 meters per second", "150,000,000 meters per second", "186,282 miles per second", "300,000,000 meters per second"],
    "Who painted the ceiling of the Sistine Chapel?": ["Michelangelo", "Raphael", "Leonardo da Vinci", "Donatello"],
    "What is the capital of Australia?": ["Canberra", "Sydney", "Melbourne", "Brisbane"],
    "Who wrote 'Pride and Prejudice'?": ["Jane Austen", "Charlotte Bronte", "Mary Shelley", "Emily Dickinson"],
    "What is the chemical symbol for oxygen?": ["O", "O2", "Ox", "Og"],
    "What is the largest mammal in the world?": ["Blue Whale", "Elephant", "Giraffe", "Hippopotamus"],
    "What is the smallest bone in the human body?": ["Stapes", "Femur", "Tibia", "Fibula"],
    "Who invented the telephone?": ["Alexander Graham Bell", "Thomas Edison", "Nikola Tesla", "Guglielmo Marconi"],
    "What is the primary language spoken in Brazil?": ["Portuguese", "Spanish", "French", "English"],
    "What is the result of 1 + 1?": ["2", "3", "1", "0"],
    "What is the capital of Canada?": ["Ottawa", "Toronto", "Vancouver", "Montreal"]
}

MAX_SCORE = 10   
questions = random.sample(list(QUESTIONS.items()), 10)
score = 0
message_label = None
answer_selected = None

# Defining all functions

def display_frame(frame):
  frame.tkraise()  

def create_welcome_frame():
  welcome_frame = Frame(container, bg = 'gray12')
  welcome_frame.grid(row = 0, column = 0, sticky = 'nsew')

  title_label = Label(welcome_frame, text = "QuizApp", font = ('Arial', 24), fg = 'white', bg = 'gray12')
  title_label.pack(fill = X, pady = 30)

  welcome_label = Label(welcome_frame, text = 'Welcome to QuizApp! Press the 'Start' button to begin the quiz.', font = ('Arial', 20), fg = 'white', bg = 'gray12', pady = 40)
  welcome_label.pack(pady = 20)

  start_button = Button(welcome_frame, text = 'Start', font = ('Arial', 24), fg = 'white', bg = 'SpringGreen2', padx = 20, pady = 20, command = generate_quiz_frame)
  start_button.pack(pady = (60, 0))

  return welcome_frame

def create_quiz_frame():
  quiz_frame = Frame(container, bg = 'gray12')
  quiz_frame.grid(row = 0, column = 0, sticky = 'nsew')

  return quiz_frame

def generate_quiz():
  global questions, answer_selected

  if questions:
    global question

    question, answers = questions.pop(0)
    correct_answer = answers[0]

    question_label = Label(quiz_frame, text = question, font = ('Arial', 25), fg = 'white', bg = 'gray12', pady = 40)
    question_label.pack(anchor = W, padx = (200, 0))

    random.shuffle(answers)

    for answer in answers:
      answer_selected = False

      answer_button = Button(quiz_frame, text = answer, width = 30, pady = 17, font = ('Arial', 19), command = lambda chosen_answer = answer: display_quiz_message(chosen_answer, correct_answer))
      answer_button.pack(anchor = W, padx = (200, 0), pady = 10)
  else:
    display_frame(results_frame)
    update_results_frame()

def display_quiz_message(chosen_answer, correct_answer):
  global message_label, score, answer_selected

  if not answer_selected:

    if message_label:
      message_label.destroy()

    message = ''
    message_color = ''
    answer_selected = True

    if chosen_answer == correct_answer:
      message = 'You got it correct!'
      message_color = 'SpringGreen3'
      score += 1
    else:
      message = 'You got it wrong...'
      message_color = 'red2'

    message_label = Label(quiz_frame, text = message, font = ('Arial', 20), fg = message_color, bg = 'gray12')
    message_label.pack(anchor = W, padx = (200, 0))

    next_button = Button(quiz_frame, text = 'Next', font = ('Arial', 15), command = lambda: clear_frame(quiz_frame))
    next_button.pack(anchor = W, padx = (200, 0), pady = (20,0))
  else:
    pass

def clear_frame(frame):
  for widget in frame.winfo_children():
    widget.destroy()

  generate_quiz()

def generate_quiz_frame():
  generate_quiz()
  display_frame(quiz_frame)

def create_results_frame():
  
  results_frame = Frame(container, bg = 'gray12')
  results_frame.grid(row = 0, column = 0, sticky = 'nsew')

  return results_frame

def update_results_frame():
  for widget in results_frame.winfo_children():
    widget.destroy()

  results_label = Label(results_frame, text = f"Your score: {score} / {MAX_SCORE}", font = ('Arial', 20), fg = 'white', bg = 'gray12', pady = 40)
  results_label.pack(anchor = CENTER)

  button = Button(results_frame, text = 'Back to main menu', font = ('Arial', 24), fg = 'white', bg = 'SpringGreen2', padx = 20, pady = 20, command = reset_quiz)
  button.pack(anchor = CENTER, pady = (60,0))

def reset_quiz():
  global questions, score, answer_selected
  questions = random.sample(list(QUESTIONS.items()), 10)
  score = 0
  answer_selected = None
  display_frame(welcome_frame)

# Main app window
master = Tk()
master.title('QuizApp')
master.config(bg = 'gray12')

# Container for all frames
container = Frame(master, bg = 'gray12')
container.pack(fill = BOTH, expand = True)

container.grid_rowconfigure(0, weight = 1)
container.grid_columnconfigure(0, weight = 1)

welcome_frame = create_welcome_frame()
quiz_frame = create_quiz_frame()
results_frame = create_results_frame()

display_frame(welcome_frame)

master.mainloop()

I tried fixing it with chatgpt but the code it generates doesn’t work properly either.
I tried closing and opening VSCode.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật