-
Notifications
You must be signed in to change notification settings - Fork 9
/
quiz_brain.py
37 lines (28 loc) · 1.24 KB
/
quiz_brain.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
class QuizBrain:
def __init__(self, questions):
self.question_no = 0
self.score = 0
self.questions = questions
self.current_question = None
def has_more_questions(self):
"""To check if the quiz has more questions"""
return self.question_no < len(self.questions)
def next_question(self):
"""Get the next question by incrementing the question number"""
self.current_question = self.questions[self.question_no]
self.question_no += 1
q_text = self.current_question.question_text
return f"Q.{self.question_no}: {q_text}"
def check_answer(self, user_answer):
"""Check the user answer against the correct answer and maintain the score"""
correct_answer = self.current_question.correct_answer
if user_answer.lower() == correct_answer.lower():
self.score += 1
return True
else:
return False
def get_score(self):
"""Get the number of correct answers, wrong answers and score percentage."""
wrong = self.question_no - self.score
score_percent = int(self.score / self.question_no * 100)
return (self.score, wrong, score_percent)