-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquiz_brain.py
61 lines (49 loc) · 2.2 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
from random import shuffle
class QuizBrain:
"""A class to manage the logic in the quiz app."""
def __init__(self, question_bank):
"""Initialize the attributes of the quiz module."""
self.question_bank = question_bank
self.question_number = 0
self.score = 0
def ask_question(self):
"""Ask the user a numbered question from a bank of questions. Returns user answer as a string."""
print(f"Question {self.question_number + 1}. {self.question_bank[self.question_number].text}\n")
answer_choices = [
self.question_bank[self.question_number].answer,
self.question_bank[self.question_number].incorrect_answer[0],
self.question_bank[self.question_number].incorrect_answer[1],
self.question_bank[self.question_number].incorrect_answer[2],
]
shuffle(answer_choices)
answer_dict = {
'1': answer_choices[0],
'2': answer_choices[1],
'3': answer_choices[2],
'4': answer_choices[3]
}
for key, value in answer_dict.items():
print(f"{key}. {value}")
return answer_dict
def questions_remaining(self):
"""Returns True if there are questions in the question bank, else returns False."""
return self.question_number < len(self.question_bank)
def check_answer(self, answer_dict):
"""Checks the users answer against the correct answer. If correct, return True. Else return False."""
try:
answer = input("\nAnswer: ").lower()
if answer_dict[answer] == self.question_bank[self.question_number].answer:
print("\nCorrect!")
self.score += 1
self.question_number += 1
return True
else:
print("\nIncorrect.")
print(f"\nThe correct answer was '{self.question_bank[self.question_number].answer}'.")
self.question_number += 1
return False
except KeyError:
print("\nInvalid selection.")
def calculate_score(self):
percent_score = int((self.score/len(self.question_bank)) * 100)
return f"{percent_score} %"