82 lines
1.9 KiB
Python
82 lines
1.9 KiB
Python
|
#!/usr/bin/python3
|
||
|
|
||
|
import pathlib
|
||
|
import time
|
||
|
import os
|
||
|
import csv
|
||
|
import random
|
||
|
from collections import Counter
|
||
|
from rich.console import Console
|
||
|
from rich.markdown import Markdown
|
||
|
|
||
|
|
||
|
def _load_csv(filepath):
|
||
|
"""Loads the csv file to a dict"""
|
||
|
dictreader = csv.DictReader(open(filepath, "r"))
|
||
|
data = []
|
||
|
for element in dictreader:
|
||
|
data.append(element)
|
||
|
return data
|
||
|
|
||
|
|
||
|
def _get_random_question(data):
|
||
|
choice = random.choice(data)
|
||
|
return choice
|
||
|
|
||
|
|
||
|
def print_random_question(data):
|
||
|
os.system("clear")
|
||
|
c = Console()
|
||
|
question = "# {}".format(data["QUESTION"])
|
||
|
question = Markdown(question)
|
||
|
c.print(question)
|
||
|
answers = []
|
||
|
for i in data.keys():
|
||
|
if i.isnumeric():
|
||
|
answers.append(data[i])
|
||
|
answer = ""
|
||
|
for i in answers:
|
||
|
answer += "1. {} \n".format(i)
|
||
|
answer = Markdown(answer)
|
||
|
c.print(answer)
|
||
|
|
||
|
|
||
|
def _test_question(data):
|
||
|
result = input("What's your answer? ")
|
||
|
if result in data["ANSWER"]:
|
||
|
return True
|
||
|
else:
|
||
|
return False
|
||
|
|
||
|
def _print_succes(status):
|
||
|
c = Console()
|
||
|
if status:
|
||
|
msg = "## Good job!"
|
||
|
else:
|
||
|
msg = "## that's not the right answer..."
|
||
|
md = Markdown(msg)
|
||
|
c.print(md)
|
||
|
|
||
|
def print_stats(counter):
|
||
|
succes = counter.count(True)
|
||
|
fail = counter.count(False)
|
||
|
print("{} out of {} correct!".format(succes, succes + fail))
|
||
|
input("press ENTER for a new question or CTRL-C to quit")
|
||
|
|
||
|
if __name__ == "__main__":
|
||
|
filepath = pathlib.Path("./data/list_book1.csv")
|
||
|
data = _load_csv(filepath)
|
||
|
try:
|
||
|
counter = []
|
||
|
while True:
|
||
|
question = _get_random_question(data)
|
||
|
print_random_question(question)
|
||
|
status = _test_question(question)
|
||
|
counter.append(status)
|
||
|
_print_succes(status)
|
||
|
print_stats(counter)
|
||
|
except KeyboardInterrupt:
|
||
|
print("byebye")
|
||
|
|
||
|
|