-
Notifications
You must be signed in to change notification settings - Fork 32
/
exam.py
executable file
·82 lines (64 loc) · 2.16 KB
/
exam.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
74
75
76
77
78
79
80
81
82
import sys
import os
import json
import random
import textwrap
from collections import Counter
if sys.version < '3':
_input = raw_input
else:
_input = input
# -----------------------------------------------------------------------------
def wrapped_out(i, s):
lead = '{0}. '.format(i)
wrapper = textwrap.TextWrapper(initial_indent=lead,
subsequent_indent=' ' * len(lead))
s = wrapper.fill(s)
print(s)
def ask(i, q):
os.system('cls' if os.name == 'nt' else 'clear')
wrapped_out(i, q['text'])
print('\n')
for k in sorted(q['options']):
wrapped_out(k, q['options'][k])
print('\n')
if sys.version < '3':
a = raw_input('> ').upper().translate(None, ' ,')
else:
a = input('> ').upper().translate({ord(' '): None, ord(','): None})
return [x for x in a]
def check(q, a):
compare = Counter(q['answers']) == Counter(a)
return 1 if compare else 0
def reveal(q, a, s):
print('Correct' if s else 'Incorrect')
if not s:
print(', '.join(sorted(q['answers'])))
_input("'Enter' to continue")
# -----------------------------------------------------------------------------
def run():
with open('questions.json') as f:
questions = json.load(f)
total = 0
# exam_length = 40
exam_length = int(sys.argv[1]) if len(sys.argv) >1 else 40
passing = (exam_length * 65) / 100
study_guide = {}
exam = random.sample(questions, exam_length)
for i, question in enumerate(exam):
answer = ask(i + 1, question)
score = check(question, answer)
total += score
#reveal(question, answer, score)
correct_answers = ''
for correct_answer in question['answers']:
correct_answers+= " : "+question['options'][correct_answer]
if not score:
study_guide[i+1] = question['text']+" (answers"+correct_answers+")"
print('Your score: {0} of {1}'.format(total, exam_length))
print('Passed!' if total >= passing else 'Failed')
print('\nMisses\n')
for i,review in study_guide.items():
wrapped_out(i, review)
if __name__ == '__main__':
run()