-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTask5_quiz.py
More file actions
80 lines (67 loc) · 2.98 KB
/
Task5_quiz.py
File metadata and controls
80 lines (67 loc) · 2.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import tkinter as tk
from tkinter import messagebox
import random
class QuizGame:
def __init__(self):
self.window = tk.Tk()
self.window.title("Quiz Game")
self.window.geometry("400x300")
self.questions = [
{
"question": "What is the capital of France?",
"options": ["Paris", "London", "Berlin", "Rome"],
"correct_answer": "Paris"
},
{
"question": "What is the largest planet in our solar system?",
"options": ["Earth", "Saturn", "Jupiter", "Uranus"],
"correct_answer": "Jupiter"
},
{
"question": "Who painted the Mona Lisa?",
"options": ["Leonardo da Vinci", "Michelangelo", "Raphael", "Caravaggio"],
"correct_answer": "Leonardo da Vinci"
},
{
"question": "What is the chemical symbol for gold?",
"options": ["Ag", "Au", "Hg", "Pb"],
"correct_answer": "Au"
},
{
"question": "What is the smallest country in the world?",
"options": ["Vatican City", "Monaco", "Nauru", "Tuvalu"],
"correct_answer": "Vatican City"
}
]
self.current_question = random.choice(self.questions)
self.score = 0
self.question_label = tk.Label(self.window, text=self.current_question["question"], wraplength=400)
self.question_label.pack()
self.option_buttons = []
for i, option in enumerate(self.current_question["options"]):
button = tk.Button(self.window, text=option, command=lambda option=option: self.check_answer(option))
button.pack()
self.option_buttons.append(button)
self.score_label = tk.Label(self.window, text="Score: 0")
self.score_label.pack()
def check_answer(self, user_answer):
if user_answer == self.current_question["correct_answer"]:
self.score += 1
messagebox.showinfo("Correct", "Correct answer!")
else:
messagebox.showinfo("Incorrect", f"Incorrect answer. The correct answer is {self.current_question['correct_answer']}.")
self.score_label.config(text=f"Score: {self.score}")
self.current_question = random.choice(self.questions)
self.question_label.config(text=self.current_question["question"])
for button in self.option_buttons:
button.pack_forget()
self.option_buttons = []
for i, option in enumerate(self.current_question["options"]):
button = tk.Button(self.window, text=option, command=lambda option=option: self.check_answer(option))
button.pack()
self.option_buttons.append(button)
def run(self):
self.window.mainloop()
if __name__ == "__main__":
game = QuizGame()
game.run()