Menu
Coddy logo textTech

Print Results

Lesson 9 of 11 in Coddy's Quiz Generator - Python OOP Project course.

Let's deal with the quiz results, and print them.

challenge icon

Challenge

Medium

Write a method print_results inside Quiz class which gets list of boolean as input (True means the ith question was answered correctly) and prints the result in specific format.

Check out the test cases to understand the format!

Try it yourself

from abc import ABC, abstractmethod


class Question(ABC):
    @abstractmethod
    def print(self):
        pass

    @abstractmethod
    def check(self, answer):
        pass


class YesNoQuestion(Question):
    def __init__(self, question, answer):
        self.question = question
        self.answer = answer

    def print(self):
        print('[?] ' + self.question + ' (yes/no)')
    
    def check(self, answer):
        return (answer == 'yes' and self.answer) or \
            (answer == 'no' and not self.answer)


class OpenQuestion(Question):
    def __init__(self, question, answers):
        self.question = question
        self.answers = answers

    def print(self):
        print('[?] ' + self.question)
    
    def check(self, answer):
        return answer in self.answers


class MultiOptionsQuestion(Question):
    def __init__(self, question, options, answer_index):
        self.question = question
        self.options = options
        self.answer_index = answer_index

    def print(self):
        print('[?] ' + self.question)
        print()
        for i in range(len(self.options)):
            print('[' + str(i + 1) + '] ' + self.options[i])
    
    def check(self, answer):
        return self.answer_index == int(answer) - 1


class Quiz:
    def __init__(self, questions):
        self.questions = questions
    
    def start(self):
        for question in self.questions:
            question.print()
            print()
            res = question.check(input('[+] '))
            print()
            print()

All lessons in Quiz Generator - Python OOP Project