f.write('\n')
def add(self, question, answer, correct):
- if question.prompt not in self:
- self[question.prompt] = []
- self[question.prompt].append({
+ if question.id not in self:
+ self[question.id] = []
+ self[question.id].append({
'answer': answer,
'correct': correct,
})
def get_answered(self, questions):
- return [q for q in questions if q.prompt in self]
+ return [q for q in questions if q.id in self]
def get_unanswered(self, questions):
- return [q for q in questions if q.prompt not in self]
+ return [q for q in questions if q.id not in self]
def get_correctly_answered(self, questions):
return [q for q in questions
- if True in [a['correct'] for a in self.get(q.prompt, [])]]
+ if True in [a['correct'] for a in self.get(q.id, [])]]
def get_never_correctly_answered(self, questions):
return [q for q in questions
- if True not in [a['correct'] for a in self.get(q.prompt, [])]]
+ if True not in [a['correct'] for a in self.get(q.id, [])]]
class Question (object):
- def __init__(self, prompt=None, answer=None, help=None):
+ def __init__(self, id=None, prompt=None, answer=None, help=None):
+ if id is None:
+ id = prompt
+ self.id = id
self.prompt = prompt
self.answer = answer
self.help = help
def __getstate__(self):
return {
+ 'id': self.id,
'prompt': self.prompt,
'answer': self.answer,
'help': self.help,
}
def __setstate__(self, state):
+ if 'id' not in state:
+ state['id'] = state.get('prompt', None)
self.__dict__.update(state)
def check(self, answer):
def display_results(self):
print('results:')
for question in self.quiz:
- for answer in self.answers.get(question.prompt, []):
+ for answer in self.answers.get(question.id, []):
self.display_result(question=question, answer=answer)
self.display_totals()