-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmcqCli.py
71 lines (60 loc) · 2.59 KB
/
mcqCli.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
"""
Simple CLI quiz app
"""
import json
import quizMaker
class Sprint:
def __init__(self, question_set_json):
self.lookup = {"A": 0, "B": 1, "C": 2, "D": 3, "E": 4, "F": 5}
self.reverse_lookup = {0: "A", 1: "B", 2: "C", 3: "D", 4: "E", 5: "F"}
self.current = 1
with open(question_set_json) as _f:
question_set = json.load(_f)
self.question_ids, self.questions = quizMaker.make_quiz(question_set, 20, True, True, True)
self.responses = []
def run(self):
while self.current <= len(self.questions):
self.question()
self.current += 1
print(self.grade())
def question(self):
q = self.questions[self.current - 1]
print(q["text"])
for count, answer in enumerate(q["answers"]):
print(f"{self.reverse_lookup[count]}. {answer[1]}")
response = self.keyed_response(len(q["answers"]), self.parse_selections(input()))
self.responses.append(response)
self.review(response, q["answers"])
def parse_selections(self, answer: str):
"""Returns a list of selected answer choice indices."""
labels = list(set([label.upper() for label in answer if label.upper() in "ABCDEF"]))
labels.sort()
return [self.lookup[label] for label in labels]
def keyed_response(self, choice_count, selections):
response = [0 for i in range(choice_count)]
for i in selections:
response[i] = 1
return response
def review(self, response, answers):
if response == [a[0] for a in answers]:
print("Correct.")
else:
for i in range(len(answers)):
if answers[i][0]:
if response[i]:
print(f"{self.reverse_lookup[i]} is correct.")
else:
print(f"{self.reverse_lookup[i]} should have been selected.")
elif not answers[i][0] and response[i]:
print(f"{self.reverse_lookup[i]} is not correct.")
def grade(self):
"""Returns score out of 1000."""
points = 0
for i in range(len(self.questions)):
correct_answer_count = sum([a[0] for a in self.questions[i]["answers"]])
correct_selections = 0
for count, response in enumerate(self.responses[i]):
if response and self.questions[i]["answers"][count][0]:
correct_selections += 1
points += round((1000/len(self.questions)) * (correct_selections/correct_answer_count))
return points