Menu
Coddy logo textTech

Open Question

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

challenge icon

Challenge

Easy

Create a class OpenQuestion which inherits from Question and implements constructor and the methods print and check from Question class.

 

Constructor - should get string and list of strings and save them under the fields question and answers accordingly.

 

print method - prints the question in the format '[?] {question}'

For example, if the question is 'Are you experienced programmer?', the method should print '[?] Are you experienced programmer?'

 

check method - returns True if input string (the answer) included in answers field, otherwise False.

For example, for answers = ['yes', 'maybe'] if check('yes') return True, if check('nope') return False 

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)

All lessons in Quiz Generator - Python OOP Project