From: W. Trevor King Date: Tue, 5 Feb 2013 15:45:11 +0000 (-0500) Subject: ui.cli: Rework display_result() for multiple answers per question X-Git-Tag: v0.1~64 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=04818c0305faa3db90cfa447e99d1767084b94b1;p=quizzer.git ui.cli: Rework display_result() for multiple answers per question --- 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)