-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquiz_brain.py
73 lines (63 loc) · 2.74 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
62
63
64
65
66
67
68
69
70
71
72
73
from question_model import Question
from random import shuffle
from prettytable import PrettyTable
from os import system
class QuizBrain():
def __init__(self, databank):
self.question_list = []
for question in databank:
self.question_list.append(
Question(
qcategory=question["category"],
qtype=question["type"],
qdifficulty=question["difficulty"],
qquestion=question["question"],
qcorans=question["correct_answer"],
qincans=question["incorrect_answers"],
)
)
self.player_wrong = 0
self.player_right = 0
def ask_question(self, current_question):
print(current_question.question)
if current_question.is_boolean():
player_response = input("\nTrue or False?\n").lower()
if current_question.is_answer(player_response):
return True
else:
return False
else:
possible_answers = []
possible_answers.append(current_question.correct_answer)
for answer in current_question.incorrect_answers:
possible_answers.append(answer)
shuffle(possible_answers)
question_dic = current_question.present_answers(possible_answers)
question_table = PrettyTable()
question_table.add_column("",list(question_dic))
question_table.add_column("Answer",possible_answers)
print(question_table)
player_response = input("What is your answer? A, B, C, or D\n").upper()
while not player_response.isalpha() or len(player_response) != 1:
player_response = input("What is your answer? A, B, C, or D\n").upper()
if player_response in question_dic:
if current_question.is_answer(question_dic[player_response]):
return True
else:
return False
else:
return False
def take_quiz(self):
print("Welcome to the Quiz Game!")
for question in self.question_list:
if self.ask_question(question):
self.player_right += 1
system("cls")
print("Correct!\n")
else:
self.player_wrong += 1
system("cls")
print("Incorrect. The correct answer was " + question.correct_answer + ".\n")
accuracy = str(int(round(self.player_right/(self.player_right+self.player_wrong)*100,0)))
total_qs = str(len(self.question_list))
print(f"Your score is {accuracy}% out of {total_qs} questions.")