#!/usr/bin/python3 import os import csv import random from rich.console import Console from rich.markdown import Markdown # Open the CSV file and create a dictionary with all the questions def _create_dictionary(): with open(path) as file: reader = csv.DictReader(file) data = list(reader) return data def _print_question(question_asked): """ :param question_asked: :return: """ os.system("clear") c = Console() actual_question = "# {}".format(question_asked["QUESTION"]) actual_question = Markdown(actual_question) c.print(actual_question) answers = [] for i in question_asked.keys(): if i.isnumeric(): answers.append(question_asked[i]) answer = "" for i in answers: answer += "1. {} \n".format(i) answer = Markdown(answer) c.print(answer) def _has_multiple_answer(question_asked): """ :param question_asked: :return Boolean: """ answers = str(question_asked["ANSWER"]).split() try: if answers[1]: return True except: return False def _one_digit_answer(question_asked): """ :param question_asked: :return Boolean: """ result = input("What's your answer? ") if result == question_asked["ANSWER"]: return True else: return False def _multiple_digit_answer(question_asked): """ :param question_asked: :return Boolean: """ answers = input("What's your answer? ").split(" ") right_answers = str(question_asked["ANSWER"]).split(" ") if len(answers) == 0: return False for answer in answers: try: test = right_answers.index(answer) right_answers.pop(test) except: return False if len(right_answers) == 0: return True else: return False def _test_question(question_asked): """ :param question_asked: :return Boolean: """ if _has_multiple_answer(question_asked): return _multiple_digit_answer(question_asked) else: return _one_digit_answer(question_asked) def _print_success(result): """ :param result: """ c = Console() if result: msg = "## Good job!" else: msg = "## that's not the right answer..." md = Markdown(msg) c.print(md) def print_stats(counter): """ :param counter: """ success = counter.count(True) fail = counter.count(False) print("{} out of {} correct!".format(success, success + fail)) input("press ENTER for a new question or CTRL-C to quit") if __name__ == "__main__": path = "./data/list_book1.csv" counter = [] dictionary = _create_dictionary() random.shuffle(dictionary) for question in dictionary: _print_question(question) result = _test_question(question) counter.append(result) _print_success(result) print_stats(counter)