-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuizzer.py
More file actions
127 lines (105 loc) · 4.7 KB
/
Quizzer.py
File metadata and controls
127 lines (105 loc) · 4.7 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#Quiz Game with OOP
import requests
import html
import tkinter
import textwrap
#Fetching questions from Open Trivia Database API
params = {
"amount": 10,
"category": 18,
"type": "boolean"
}
response = requests.get("https://opentdb.com/api.php", params=params)
database = response.json()
quiz_questions = database["results"]
class Question:
def __init__(self, question, answer): #Attributes of the class
self.ques_num = 0
self.question = html.unescape(question)
self.answer = answer
def check_answer(self, user_answer): #To check if the user's answer is correct
if user_answer.lower() == str(self.answer).lower():
return True
else:
return False
def set_question_number(self, num): #To set the question number
self.ques_num = num
def __str__(self): #To print the question to the user
return f"Q{self.ques_num}: {self.question} (True/False)"
class Quiz:
def __init__(self, questions):
self.questions = questions
self.score = 0
self.current_question_index = 0
def next_question(self):
if self.current_question_index < len(self.questions):
question = self.questions[self.current_question_index]
question.set_question_number(self.current_question_index + 1)
self.current_question_index += 1
return question
else:
return None
def check_answer(self, user_answer):
current_question = self.questions[self.current_question_index - 1]
if current_question.check_answer(user_answer):
self.score += 1
return True
else:
return False
def get_score(self):
return self.score
def reset_quiz(self):
self.score = 0
self.current_question_index = 0
class QuizApp:
def __init__(self, quiz):
self.quiz = quiz
self.window = tkinter.Tk()
self.window.title("Quizzer")
self.window.config(bg="#E5BEED")
self.window.resizable(False, False)
self.window_setup()
self.window.mainloop()
def window_setup(self):
self.score_label = tkinter.Label(self.window, text=f"Score: {self.quiz.get_score()}/{len(self.quiz.questions)}", font=("Arial", 12, "bold"), bg="#E5BEED", fg="black",highlightthickness=0)
self.score_label.grid(row=0, column=1, sticky="w", padx=20, pady=20)
self.question_screen = tkinter.Canvas(width=300, height=250, bg="#9593D9", highlightthickness=1 , highlightbackground="#000000")
self.question_screen.grid(row=1, column=0, columnspan=2, padx=20, pady=20)
self.true_button = tkinter.Button(self.window, text="True", command=lambda: self.check_answer("True"), width=10, fg="green")
self.true_button.grid(row=2, column=0, padx=20, sticky="e", pady=20)
self.false_button = tkinter.Button(self.window, text="False", command=lambda: self.check_answer("False"), width=10, fg="red")
self.false_button.grid(row=2, column=1, padx=20, pady=20, sticky="w")
self.next_question()
def next_question(self):
question = self.quiz.next_question()
if question:
self.question_screen.delete("all")
text = textwrap.fill(str(question), width=27)
self.question_screen.create_text(150, 125 , text=text, fill="white", font=("Arial", 15, "italic"))
self.score_label.config(text=f"Score: {self.quiz.get_score()}/{len(self.quiz.questions)}")
else:
self.question_screen.delete("all")
text = textwrap.fill(f"Quiz Over! Your final score is: {self.quiz.get_score()}/{len(self.quiz.questions)}", width=27)
self.question_screen.create_text(150, 125 , text=text, fill="white", font=("Arial", 15, "italic"))
self.true_button.config(state="disabled")
self.false_button.config(state="disabled")
restart_button = tkinter.Button(self.window, text="Restart Quiz", command=self.restart_quiz)
restart_button.grid(row=3, column=0, columnspan=2, pady=20)
def check_answer(self, user_answer):
if self.quiz.check_answer(user_answer):
print("Correct!")
else:
print("Incorrect!")
self.next_question()
def restart_quiz(self):
self.quiz.reset_quiz()
for widget in self.window.winfo_children():
widget.destroy()
self.window_setup()
def main():
global quiz_questions
print("Welcome to the Quizzer!'.\n")
print("There are 10 questions in total.")
quiz = Quiz([Question(q["question"], q["correct_answer"]) for q in quiz_questions])
QuizApp(quiz)
main()