From: W. Trevor King Date: Tue, 5 Feb 2013 14:29:58 +0000 (-0500) Subject: Add .display_results() for showing users their results and scores X-Git-Tag: v0.1~73 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=13125aff1b7bd1d61cea6ab60a904e30b91b1f38;p=quizzer.git Add .display_results() for showing users their results and scores --- 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))