from . import __doc__ as _module_doc
from . import __version__
from . import quiz as _quiz
+from .ui import cli as _cli
def main():
quiz = _quiz.Quiz(path=args.quiz, encoding=encoding)
quiz.load()
- for question in quiz:
- print(question)
+ ui = _cli.CommandLineInterface(quiz=quiz)
+ ui.run()
--- /dev/null
+class UserInterface (object):
+ "Give a quiz over a generic user interface"
+ def __init__(self, quiz=None, answers=None):
+ self.quiz = quiz
+ if answers is None:
+ answers = {}
+ self.answers = answers
+
+ def run(self):
+ raise NotImplementedError()
+
+ def get_question(self):
+ return self.quiz[0]
+
+ def process_answer(self, question, answer):
+ if question not in self.answers:
+ self.answers[question] = []
+ correct = question.check(answer)
+ self.answers[question].append({
+ 'answer': answer,
+ 'correct': correct,
+ })
+ return correct
--- /dev/null
+from . import UserInterface
+
+
+class CommandLineInterface (UserInterface):
+ def run(self):
+ while True:
+ question = self.get_question()
+ if not question:
+ break
+ print(question.prompt)
+ while True:
+ answer = input('? ')
+ a = answer.strip().lower()
+ if a in ['q', 'quit']:
+ return
+ if a in ['?', 'help']:
+ print()
+ print(question.prompt)
+ print(question.hint)
+ continue
+ break
+ correct = self.process_answer(question=question, answer=answer)
+ if correct:
+ print('correct\n')
+ else:
+ print('incorrect\n')