From 04818c0305faa3db90cfa447e99d1767084b94b1 Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Tue, 5 Feb 2013 10:45:11 -0500 Subject: [PATCH] ui.cli: Rework display_result() for multiple answers per question --- quizzer/ui/cli.py | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/quizzer/ui/cli.py b/quizzer/ui/cli.py index a82843f..b28d89f 100644 --- a/quizzer/ui/cli.py +++ b/quizzer/ui/cli.py @@ -32,19 +32,24 @@ class CommandLineInterface (UserInterface): def display_results(self): print('results:') for question in self.quiz: - for answer in self.answers.get(question.id, []): - self.display_result(question=question, answer=answer) + if question.id in self.answers: + self.display_result(question=question) + print() self.display_totals() - def display_result(self, question, answer): - if answer['correct']: - correct = 'correct' - else: - correct = 'incorrect' + def display_result(self, question): + answers = self.answers.get(question.id, []) print('question: {}'.format(question.prompt)) - print('you answered: {}'.format(answer['answer'])) - print('which was: {}'.format(correct)) - print() + la = len(answers) + lc = len([a for a in answers if a['correct']]) + print('answers: {}/{} ({:.2f})'.format(lc, la, float(lc)/la)) + for answer in answers: + if answer['correct']: + correct = 'correct' + else: + correct = 'incorrect' + print(' you answered: {}'.format(answer['answer'])) + print(' which was: {}'.format(correct)) def display_totals(self): answered = self.answers.get_answered(questions=self.quiz) -- 2.26.2