From 13125aff1b7bd1d61cea6ab60a904e30b91b1f38 Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Tue, 5 Feb 2013 09:29:58 -0500 Subject: [PATCH] Add .display_results() for showing users their results and scores --- quizzer/ui/__init__.py | 16 +++++++++++++++- quizzer/ui/cli.py | 10 ++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/quizzer/ui/__init__.py b/quizzer/ui/__init__.py index 3dc520e..69b1203 100644 --- a/quizzer/ui/__init__.py +++ b/quizzer/ui/__init__.py @@ -13,7 +13,7 @@ class UserInterface (object): raise NotImplementedError() def get_question(self): - remaining = [q for q in self.quiz if q not in self.answers] + remaining = self.get_unanswered() if remaining: return remaining[0] @@ -26,3 +26,17 @@ class UserInterface (object): 'correct': correct, }) return correct + + def get_answered(self): + return [q for q in self.quiz if q in self.answers] + + def get_unanswered(self): + return [q for q in self.quiz if q not in self.answers] + + def get_correctly_answered(self): + return [q for q in self.quiz + if True in [a['correct'] for a in self.answers.get(q, [])]] + + def get_never_correctly_answered(self): + return [q for q in self.quiz + if True not in [a['correct'] for a in self.answers.get(q, [])]] diff --git a/quizzer/ui/cli.py b/quizzer/ui/cli.py index 797a215..12b85bd 100644 --- a/quizzer/ui/cli.py +++ b/quizzer/ui/cli.py @@ -30,6 +30,7 @@ class CommandLineInterface (UserInterface): if question in self.answers: for answer in self.answers[question]: self.display_result(question=question, answer=answer) + self.display_totals() def display_result(self, question, answer): if answer['correct']: @@ -40,3 +41,12 @@ class CommandLineInterface (UserInterface): print('you answered: {}'.format(answer['answer'])) print('which was: {}'.format(correct)) print() + + def display_totals(self): + answered = self.get_answered() + correctly_answered = self.get_correctly_answered() + la = len(answered) + lc = len(correctly_answered) + print('answered {} of {} questions'.format(la, len(self.quiz))) + print(('of the answered questions, {} ({:.2f}) were answered correctly' + ).format(lc, float(lc)/la)) -- 2.26.2