Added random_question.py with basic usage (#164)
This commit is contained in:
parent
8cc94ceb78
commit
c4e7308f25
50
scripts/random_question.py
Normal file
50
scripts/random_question.py
Normal file
@ -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('<summary>') + 9
|
||||
question_end = text.find('</summary>')
|
||||
answer_end = text.find('</b></details>')
|
||||
|
||||
if answer_end == -1:
|
||||
break
|
||||
|
||||
question = text[question_start: question_end].replace('<br>', '').replace('<b>', '')
|
||||
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()
|
Loading…
Reference in New Issue
Block a user