Matrices-Practice-Tool / src / QuizClasses / quiz.py
quiz.py
Raw
'''Quiz objects'''
import random
import sqlite3
from QuestionClasses.question import Question


class Quiz:
    '''Class for holding quiz data'''
    start_time = 0
    questions = []
    time_taken = 0
    score = 0

    def __init__(self, question_types: list, start_time: int):
        '''Initialiser'''
        self.questions = []
        score = 0
        self.generate_quiz(question_types)
        self.start_time = start_time

    def generate_quiz(self, question_types: list):
        '''Generate the questions'''
        constructors = {'type_1': Question.random_add,
                        'type_2': Question.random_sub,
                        'type_3': Question.random_mult,
                        'type_4': Question.random_inv,
                        'type_5': Question.random_det}
        for _ in range(10):
            q_type = random.choice(question_types)
            constructor = constructors[q_type]
            self.questions.append(constructor())

    def set_time(self, end_time: int):
        '''Calculate time taken'''
        self.time_taken = end_time - self.start_time

    def write_to(self, username, date, edited_questions):
        for q in edited_questions:
            if 'O' in q:
                self.score += 1
                print(self.score)
        put = '''INSERT INTO QuizDetails
                 VALUES (?,?,?,?,?);'''
        connection = sqlite3.connect("QuizDatabase.db")
        cursor = connection.cursor()
        cursor.execute(put, (username, date, ''.join(edited_questions),
                             self.score, self.time_taken))
        connection.commit()
        connection.close()