Add .display_results() for showing users their results and scores
authorW. Trevor King <wking@tremily.us>
Tue, 5 Feb 2013 14:29:58 +0000 (09:29 -0500)
committerW. Trevor King <wking@tremily.us>
Tue, 5 Feb 2013 14:32:27 +0000 (09:32 -0500)
quizzer/ui/__init__.py
quizzer/ui/cli.py

index 3dc520e42e6b158aad7004545dd2bab701d789ed..69b120315e463074075c3e91e6e0c84a770441f7 100644 (file)
@@ -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, [])]]
index 797a215527095ba551e20712aa2cf098d76bacfb..12b85bd5b31fc5ab36f4f7ddb4b8a954cb2afeb5 100644 (file)
@@ -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))