From: W. Trevor King Date: Thu, 14 Feb 2013 14:17:08 +0000 (-0500) Subject: ui.cli: Avoid divide-by-zero errors if no questions were answered X-Git-Tag: v0.2~5 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=a39b2eb914bed1722877da50260a3b44f61f41f5;p=quizzer.git ui.cli: Avoid divide-by-zero errors if no questions were answered --- diff --git a/quizzer/ui/cli.py b/quizzer/ui/cli.py index 7d7c701..4a0fa49 100644 --- a/quizzer/ui/cli.py +++ b/quizzer/ui/cli.py @@ -162,7 +162,8 @@ class CommandLineInterface (UserInterface): question.format_prompt(newline='\n ')))) la = len(answers) lc = len([a for a in answers if a['correct']]) - print('answers: {}/{} ({:.2f})'.format(lc, la, float(lc)/la)) + if la: + print('answers: {}/{} ({:.2f})'.format(lc, la, float(lc)/la)) for answer in answers: if answer['correct']: correct = 'correct' @@ -182,5 +183,7 @@ class CommandLineInterface (UserInterface): 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)) + if la: + print(('of the answered questions, ' + '{} ({:.2f}) were answered correctly' + ).format(lc, float(lc)/la))