Menu
Coddy logo textTech

Domanda a scelta multipla

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

challenge icon

Sfida

Medio

Crea una classe MultiOptionsQuestion che eredita da Question e implementa il costruttore e i metodi print e check della classe Question.

 

Costruttore - deve ricevere una stringa, un elenco di stringhe e un intero, e salvarli rispettivamente nei campi question, options  e answer_index.

 

Metodo print - stampa la domanda nel formato:

[?] {question}

[1] {options[0]}
[2] {options[1]}
.
.
.

Ad esempio, se la domanda è 'How many states are in the USA?' e le opzioni sono ['49', '50', '51', '32'],

[?] How many states are in the USA?

[1] 49
[2] 50
[3] 51
[4] 32

 

Metodo check - restituisce True se la stringa di input (la risposta) è uguale a answer_index + 1, altrimenti False

Nota: l'input del metodo check è una stringa, devi convertirla in un intero!

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

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

Esercitati da solo: Compilatore Python online