-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathquestion_prompt.py
More file actions
55 lines (49 loc) · 2.21 KB
/
question_prompt.py
File metadata and controls
55 lines (49 loc) · 2.21 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
from typing import List, Set
from menu_prompt import MenuPrompt
from quiz_item import QuizItem
from app_settings import AppSettings
from session_result import SessionResult
class QuestionPrompt(MenuPrompt):
def __init__(self, questions: List[QuizItem], settings: AppSettings):
super().__init__()
self.questions = questions
self.correct_answer_count = 0
self.attempt_count = 0
self.settings = settings
def choose(self, items: Set[str]) -> str:
choice = super().choose(items)
self.attempt_count += 1
return choice
def generate_result(self):
session_result = SessionResult(
self.correct_answer_count,
len(self.questions) - self.correct_answer_count,
self.attempt_count
)
return session_result
def ask_question(self):
for question in self.questions:
print(question.question)
question_attempts = 0
answers: Set[str] = set(question.answers)
user_answer = self.choose(answers)
if self.attempt_count >= self.settings.attempts_per_session:
print('Attempts limit per session is reached. Aborting session.')
return self.generate_result()
question_attempts += 1
question_answered_correctly = user_answer == question.correct_answer
while not question_answered_correctly:
if question_attempts >= self.settings.attempts_per_question:
print('Attempts limit per question is reached. Showing the next question.')
break
print('Wrong answer. Choose again.')
user_answer = self.choose(answers)
if self.attempt_count >= self.settings.attempts_per_session:
print('Attempts limit per session is reached. Aborting session.')
return self.generate_result()
question_answered_correctly = user_answer == question.correct_answer
question_attempts += 1
if question_answered_correctly:
print('Correct answer')
self.correct_answer_count += 1
return self.generate_result()