#!/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(): answers = input("Choose your question list : \n 1: LPI Essentials \n 2: LPIC 1 101-500 \n") if answers == "1": with open(path1) as file: reader = csv.DictReader(file) data = list(reader) return data elif answers == "2": with open(path2) as file: reader = csv.DictReader(file) data = list(reader) return data else: print("Please enter a valid value") exit() 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 _print_explication(question): c = Console() actual_question = "# {}".format(question["EXPLICATION"]) actual_question = Markdown(actual_question) c.print(actual_question) def _test_question(question_asked): """ :param question_asked: :return Boolean: """ answers = input("What is your answer? ").split() right_answers = str(question_asked["ANSWER"]).split() if len(answers) == 0: return False, question_asked["ANSWER"] for answer in answers: try: test = right_answers.index(answer) right_answers.pop(test) except ValueError or IndexError: return False, question_asked["ANSWER"] if len(right_answers) == 0: return True, question_asked["ANSWER"] else: return False, question_asked["ANSWER"] def _print_success(result, answers): """ :param result: """ c = Console() if result: msg = "## Good job!" else: msg = "## that's not the right answer... The answer(s) was(were) ({})".format(answers) 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__": path1 = "./data/list_book1.csv" path2 = "./data/lpic1_book_101-500.csv" counter = [] dictionary = _create_dictionary() random.shuffle(dictionary) for question in dictionary: _print_question(question) result, answers = _test_question(question) counter.append(result) _print_explication(question) _print_success(result, answers) print_stats(counter)