scientific-programming-exer.../exam/ex03/exercise.py
2019-02-02 16:59:18 +01:00

42 lines
974 B
Python

from database import fetch_question, update_correct
class Question(object):
"""
This is the throwaway container types for questions.
It basically stores the question and the answer and
handles the database access.
One can fetch a (semi random (see database.fetch_question))
question from the database using Question.fetch()
and use submit_question() to both update the database and
check whether the answer was correct.
"""
__slots__ = ["question", "answer"]
def __init__(self, question, answer):
self.question = question
self.answer = answer
@classmethod
def fetch(cls):
data = fetch_question()
return cls(data[0], data[1])
def submit_answer(self, answer):
"""
Check whether ``answer`` is correct,
update the database accordingly and
return ``True`` if the answer was correct,
``False`` otherwise.
"""
correct = -1
if(answer == self.answer):
correct = 1
update_correct(self.question, correct)
return correct > 0