Pruefungskatalog/assets/question_answer_output.py

30 lines
811 B
Python

import json
from random import randrange
import re
# 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
# 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)