-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlesson_quiz.py
76 lines (60 loc) · 1.89 KB
/
lesson_quiz.py
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
"""
This module is a complete lesson in 'Mnogomov'
"""
import question
class LessonQuiz:
"""
Generate several question into one solis quiz which will be used for
handling language lessons.
"""
def __init__(self, questions):
self.questions = questions
self.current_q_idx = 0
self.score = 0
def get_score(self) -> int:
"""
Return user score after lesson
:return: integer representing user score
"""
return self.score
def reset_lesson(self) -> None:
"""
Reset score and question index
:return: None
"""
self.score = 0
self.current_q_idx = 0
def current_q(self) -> question.Question:
"""
Return data of question with specific index to be displayed
"""
return self.questions[self.current_q_idx]
def check_answers(self, user_answer: str) -> bool:
"""
Check user answers and change score according to results
:param user_answer: user answer got from 'request.form'
:return: True/False
"""
if self.current_q().correct(user_answer):
answer_result = True
self.score += 1
else:
answer_result = False
self.current_q_idx += 1
return answer_result
def is_finished(self) -> bool:
"""
Check if user has answered all questions
:return: True if all questions are answered, else False
"""
return self.current_q_idx >= len(self.questions) or self.current_q_idx >= 10
def to_dict(self) -> dict:
"""
Convert questions data into dictionary for keeping data if needed.
:return: dictionary with questions
"""
return {
"questions": [q.to_dict() for q in self.questions],
"current_q_idx": self.current_q_idx,
"score": self.score
}