Start
Lekcja 8 z 11 w kursie Generator quizów — projekt z programowania obiektowego w Pythonie w Coddy.
Wyzwanie
ŚredniDodaj do klasy Quiz metodę start, która nie przyjmuje żadnych danych wejściowych. Metoda będzie wyświetlać pytania (z pola questions) i prosić użytkownika o odpowiedź.
Sprawdź przypadki testowe, aby lepiej zrozumieć formatowanie. Użyj metody pytania print.
Każde pytanie o dane wejściowe powinno zaczynać się od '[+] '. Jeśli nie masz pewności, jak to zrobić, sprawdź podpowiedź!
Zwróć uwagę na spacje i nowe linie!
Spróbuj swoich sił
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
Wszystkie lekcje w sekcji Generator quizów — projekt z programowania obiektowego w Pythonie
Poćwicz samodzielnie: Kompilator Python online