Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import tkinter as tk
- # Initialize the window
- window = tk.Tk()
- window.title("Quiz Game")
- # Quiz questions and answers
- questions = [
- "What is the capital of France?",
- "Which planet is known as the Red Planet?",
- "Who painted the Mona Lisa?"
- ]
- answers = [
- "Paris",
- "Mars",
- "Leonardo da Vinci"
- ]
- # Variables for tracking the current question and score
- current_question = 0
- score = 0
- # Function to display the current question
- def display_question():
- question_label.config(text=questions[current_question])
- # Function to check the answer and update the score
- def check_answer():
- global current_question, score
- answer = answer_entry.get().strip()
- correct_answer = answers[current_question]
- if answer.lower() == correct_answer.lower():
- score += 1
- answer_entry.delete(0, tk.END)
- # Move to the next question or end the quiz
- if current_question < len(questions) - 1:
- current_question += 1
- display_question()
- else:
- end_quiz()
- # Function to end the quiz and display the final score
- def end_quiz():
- question_label.config(text="Quiz ended. Your score: {}/{}".format(score, len(questions)))
- answer_entry.config(state=tk.DISABLED)
- check_button.config(state=tk.DISABLED)
- # Create a label to display the question
- question_label = tk.Label(window, text="")
- question_label.pack(pady=10)
- # Create an entry widget for entering the answer
- answer_entry = tk.Entry(window, width=30)
- answer_entry.pack(pady=5)
- # Create a button to check the answer
- check_button = tk.Button(window, text="Check", command=check_answer)
- check_button.pack(pady=5)
- # Display the first question
- display_question()
- window.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement