Multiple Options Question
Lesson 6 of 11 in Coddy's Quiz Generator - Python OOP Project course.
Challenge
MediumCreate a class MultiOptionsQuestion which inherits from Question and implements constructor and the methods print and check from Question class.
Constructor - should get string, list of strings and integer, and save them under the fields question, options and answer_index accordingly.
print method - prints the question in the format,
[?] {question}
[1] {options[0]}
[2] {options[1]}
.
.
.For example, if the question is 'How many states are in the USA?' and the options are ['49', '50', '51', '32'],
[?] How many states are in the USA?
[1] 49
[2] 50
[3] 51
[4] 32
check method - returns True if input string (the answer) is equal to answer_index + 1, otherwise False
Note, the input of check method is string you must cast it to integer!
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