Menu
Coddy logo textTech

Inizio

Lezione 8 di 11 del corso Generatore di quiz - Progetto OOP in Python di Coddy.

challenge icon

Sfida

Medio

Aggiungi il metodo start, che non riceve input, alla classe Quiz; il metodo mostrerà le domande (dal campo questions) e chiederà all’utente di inserire una risposta.

Controlla i casi di test per capire meglio la formattazione; usa il metodo della domanda print.

Ogni richiesta di input deve iniziare con '[+] '; controlla il suggerimento se non sai come fare!

Fai attenzione agli spazi e alle nuove righe!

Provalo tu

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

Tutte le lezioni di Generatore di quiz - Progetto OOP in Python

Esercitati da solo: Compilatore Python online