ccpq/poc_hugo.py

101 lines
2.6 KiB
Python
Raw Normal View History

2021-05-28 11:59:44 +02:00
#!/usr/bin/python3
import os
import csv
import random
from rich.console import Console
from rich.markdown import Markdown
2021-05-28 16:01:01 +02:00
# Open the CSV file and create a dictionary with all the questions
def _create_dictionary():
2021-05-28 11:59:44 +02:00
with open(path) as file:
reader = csv.DictReader(file)
data = list(reader)
2021-05-28 16:01:01 +02:00
return data
def _print_question(question_asked):
2021-06-03 14:47:26 +02:00
"""
:param question_asked:
:return:
"""
2021-05-28 16:01:01 +02:00
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)
2021-06-03 14:47:26 +02:00
2021-06-10 16:44:08 +02:00
def _print_explication(question):
c = Console()
actual_question = "# {}".format(question["EXPLICATION"])
actual_question = Markdown(actual_question)
c.print(actual_question)
2021-06-03 14:47:26 +02:00
2021-06-10 16:44:08 +02:00
def _test_question(question_asked):
2021-06-03 14:47:26 +02:00
"""
:param question_asked:
:return Boolean:
"""
2021-06-10 16:44:08 +02:00
answers = input("What is your answer? ").split()
right_answers = str(question_asked["ANSWER"]).split()
2021-06-03 14:47:26 +02:00
if len(answers) == 0:
2021-06-10 16:44:08 +02:00
return False, question_asked["ANSWER"]
2021-06-03 14:47:26 +02:00
for answer in answers:
try:
test = right_answers.index(answer)
right_answers.pop(test)
2021-06-10 16:44:08 +02:00
except ValueError or IndexError:
return False, question_asked["ANSWER"]
2021-06-03 14:47:26 +02:00
if len(right_answers) == 0:
2021-06-10 16:44:08 +02:00
return True, question_asked["ANSWER"]
2021-06-03 14:47:26 +02:00
else:
2021-06-10 16:44:08 +02:00
return False, question_asked["ANSWER"]
2021-06-03 14:47:26 +02:00
2021-06-10 16:44:08 +02:00
def _print_success(result, answers):
2021-06-03 14:47:26 +02:00
"""
:param result:
"""
c = Console()
if result:
msg = "## Good job!"
else:
2021-06-10 16:44:08 +02:00
msg = "## that's not the right answer... The answer(s) was(were) ({})".format(answers)
2021-06-03 14:47:26 +02:00
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")
2021-05-28 11:59:44 +02:00
if __name__ == "__main__":
path = "./data/list_book1.csv"
2021-06-03 14:47:26 +02:00
counter = []
2021-05-28 16:01:01 +02:00
dictionary = _create_dictionary()
random.shuffle(dictionary)
for question in dictionary:
_print_question(question)
2021-06-10 16:44:08 +02:00
result, answers = _test_question(question)
2021-06-03 14:47:26 +02:00
counter.append(result)
2021-06-10 16:44:08 +02:00
_print_explication(question)
_print_success(result, answers)
2021-06-03 14:47:26 +02:00
print_stats(counter)