-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
66 lines (49 loc) · 1.88 KB
/
main.py
File metadata and controls
66 lines (49 loc) · 1.88 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
from quiz_engine.loader import load_questions
from quiz_engine.evaluator import evaluate_answer
from quiz_engine.scorer import calculate_score, save_score
from ui import QuizUI
class QuizController:
def __init__(self):
self.ui = QuizUI(self)
self.username = "" # stored internally, not shown
self.questions = []
self.current_q = 0
self.correct = 0
def start_quiz(self, username):
# Save the username only for the leaderboard
self.username = username.strip() if username.strip() else "Player"
# Load questions and initialize quiz
self.questions = load_questions("data/Questions.json")
self.current_q = 0
self.correct = 0
self.ui.set_total_questions(len(self.questions))
self.show_next_question()
def show_next_question(self):
if self.current_q >= len(self.questions):
self.end_quiz()
return
q = self.questions[self.current_q]
self.ui.show_question(q["question"], q["options"])
def check_answer(self, selected):
# Prevent going beyond the list
if self.current_q >= len(self.questions):
return
q = self.questions[self.current_q]
result = evaluate_answer(selected, q["answer"])
if result:
self.correct += 1
self.current_q += 1
if self.current_q >= len(self.questions):
self.end_quiz()
else:
self.show_next_question()
def end_quiz(self):
total = len(self.questions)
percentage = calculate_score(self.correct, total)
save_score(self.username, self.correct, total)
self.ui.show_result(self.correct, total, percentage)
def run(self):
self.ui.start()
if __name__ == "__main__":
app = QuizController()
app.run()