Menu
Coddy logo textTech

Start

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

challenge icon

Challenge

Medium

Add the method start which gets no input to the class Quiz, the method will show questions (from questions field) and ask for input (answer) from the user.

Check the test cases to understand better the formatting, use the question method print.

Each input ask should start with '[+] ', check hint if you are not sure how to do it!

Note the spaces and new lines!

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

All lessons in Quiz Generator - Python OOP Project