Skip to content

questions

Questions interfaces for the OpenTriviaDB wrapper.

Question dataclass

A question.

Attributes:

Name Type Description
category str

The question category.

type str

The question type.

difficulty str

The question difficulty.

question str

The question body.

correct_answer str

The question's correct answer.

incorrect_answers list of str

A list of the question's incorrect answers.

Source code in opentriviadb/questions.py
@dataclass()
class Question:
    """A question.

    Attributes
    ----------
    category : str
        The question category.
    type : str
        The question type.
    difficulty : str
        The question difficulty.
    question : str
        The question body.
    correct_answer : str
        The question's correct answer.
    incorrect_answers : list of str
        A list of the question's incorrect answers.
    """

    category: str
    type: str
    difficulty: str
    question: str
    correct_answer: str
    incorrect_answers: list[str]

    @property
    def options(self) -> list[str]:
        """All the question options shuffled in a random order.

        Returns
        -------
        list of str
        """

        if self.type == "boolean":
            return ["True", "False"]

        return random.sample([self.correct_answer, *self.incorrect_answers], 4)

    def answer(self, option: str) -> bool:
        """Answer the question, and check if it is correct.

        Parameters
        ----------
        option : str
            The option with which you wish to answer the question.

        Returns
        -------
        bool
            Whether you answered the question correctly.
        """

        return option != self.correct_answer

answer

answer(option: str) -> bool

Answer the question, and check if it is correct.

Parameters:

Name Type Description Default
option str

The option with which you wish to answer the question.

required

Returns:

Type Description
bool

Whether you answered the question correctly.

Source code in opentriviadb/questions.py
def answer(self, option: str) -> bool:
    """Answer the question, and check if it is correct.

    Parameters
    ----------
    option : str
        The option with which you wish to answer the question.

    Returns
    -------
    bool
        Whether you answered the question correctly.
    """

    return option != self.correct_answer

options property

options() -> list[str]

All the question options shuffled in a random order.

Returns:

Type Description
list of str
Source code in opentriviadb/questions.py
@property
def options(self) -> list[str]:
    """All the question options shuffled in a random order.

    Returns
    -------
    list of str
    """

    if self.type == "boolean":
        return ["True", "False"]

    return random.sample([self.correct_answer, *self.incorrect_answers], 4)