Pruefungskatalog/assets/question_answer_output.py

30 lines
811 B
Python
Raw Normal View History

2022-03-22 23:09:39 +01:00
import json
from random import randrange
2022-03-24 09:18:02 +01:00
import re
2022-03-22 23:09:39 +01:00
# Einlesen vom Json File
def read_json(filename):
with open(filename,encoding='utf-8') as filename:
data = json.load(filename)
return data
# Zufällige Frage erstellen
def random_question(filename):
min_ = 0
max_ = len(read_json(filename))
random_number = randrange(min_,max_)
questions = read_json(filename)
question = f'Frage #{random_number}: ' + questions[random_number][str(random_number)]['question']
answer = questions[random_number][str(random_number)]['answer']
return question, answer
2022-03-24 09:18:02 +01:00
# Ausgabe der Liste mit allen Fragen/Antworten
def complete_list(filename):
data = read_json(filename)
for obj in data:
obj_string = re.sub('[{}]', '', str(obj))
print('\n' + obj_string)