scientific-programming-exer.../exam/ex03/exercise.py

42 lines
974 B
Python
Raw Permalink Normal View History

2019-01-31 18:53:49 +00:00
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