From c4e7308f252666742e2cc276f240208dd450690e Mon Sep 17 00:00:00 2001 From: sha016 <92833633+sha016@users.noreply.github.com> Date: Wed, 20 Oct 2021 02:10:44 -0500 Subject: [PATCH] Added random_question.py with basic usage (#164) --- scripts/random_question.py | 50 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 scripts/random_question.py diff --git a/scripts/random_question.py b/scripts/random_question.py new file mode 100644 index 0000000..691125d --- /dev/null +++ b/scripts/random_question.py @@ -0,0 +1,50 @@ +import random +import optparse + + +def main(): + """ Reads through README.md for question/answer pairs and adds them to a list to randomly select from and quiz yourself. + - supports skipping quesitons with no documented answer with the -s flag + """ + parser = optparse.OptionParser() + parser.add_option("-s", "--skip", action="store_true",help="skips questions without an answer.", default=False) + options, args = parser.parse_args() + + with open('README.md', 'r') as f: + text = f.read() + + questions = [] + + while True: + question_start = text.find('') + 9 + question_end = text.find('') + answer_end = text.find('') + + if answer_end == -1: + break + + question = text[question_start: question_end].replace('
', '').replace('', '') + answer = text[question_end + 17: answer_end] + questions.append((question, answer)) + text = text[answer_end + 1:] + + num_questions = len(questions) + + while True: + try: + question, answer = questions[random.randint(0, num_questions)] + + if options.skip and not answer.strip(): + continue + + if input(f'Q: {question} ...Show answer? "y" for yes: ').lower() == 'y': + print('A: ', answer) + + except KeyboardInterrupt: + break + + print("\nGoodbye! See you next time.") + + +if __name__ == '__main__': + main()