-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexam_simulator.py
53 lines (41 loc) · 1.64 KB
/
exam_simulator.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
import datetime
from teacher import Teacher
class Exam:
def __init__(self, student_view, answers, store_test=False, topic=""):
self.student_view = student_view
self.answers = answers
if store_test:
self.store_test(topic)
def take(self):
answers = {}
for question, question_view in self.student_view.items():
print(question_view)
answer = input("Enter your answer: ")
answers[question] = answer
return answers
def grade(self, answers):
correct_answers = 0
for question, answer in answers.items():
if answer.upper() == self.answers[question].upper()[16]:
correct_answers+=1
grade = 100 * correct_answers / len(answers)
if grade < 60:
passed = "Not passed!"
else:
passed = "Passed!"
return f"{correct_answers} out of {len(answers)} correct! You achieved: {grade} % : {passed}"
def store_test(self, topic):
with open(f'Test_{topic}_{datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S")}.txt', "w") as file:
for question, question_view in self.student_view.items():
file.write(question_view)
file.write("\n")
file.write(self.answers[question])
file.write("\n")
if __name__ == "__main__":
teacher = Teacher()
student_view, answers = teacher.create_full_test()
exam = Exam(student_view, answers, store_test=True, topic=teacher.test_creator.topic)
student_answers = exam.take()
print(student_answers)
grade = exam.grade(student_answers)
print(grade)