I create a quiz using json files as categories. I would like to have random categories and random questions. I managed to have random categories already but still can’t figure out how to create drawing questions from json file.
I have 4 questions in my json file and for example I would like to draw 2 from them.
while True:
if len(the_filenames) != 0:
random_section = random.choice(the_filenames)
print()
print("Wylosowano dział:", random_section[:-5])
with open(r"C:UsersTymekPycharmProjectspythonProject1quiz!ematyka\" + random_section) as file:
questions = json.load(file)
the_filenames.remove(random_section)
i = []
for i in range(0, len(questions)):
show_questions(questions[i])
print("Twój aktualny wynik to:", points, "/", total_points)
if len(the_filenames) == 0:
{"questions": [
{
"pytanie":"Ktore dzialanie wykonamy jako pierwsze?",
"a": "dzielenie",
"b": "odejmowanie",
"c": "dodawanie",
"d": "potęgowanie",
"prawidlowa_odpowiedz":"d"
},
{
"pytanie":"Podaj wynik dzialania 18-9/3*2",
"a": 12,
"b": 6,
"c": 16,
"d": 24,
"prawidlowa_odpowiedz":"a"
},
{
"pytanie":"Ktora z wymienionych nie jest funkcja trygonometryczna?",
"a": "tangens",
"b": "sangens",
"c": "cosinus",
"d": "cotangens",
"prawidlowa_odpowiedz":"b"
},
{
"pytanie":"Suma katow w trojkącie wynosi?",
"a": "160 stopni",
"b": "360 stopni",
"c": "90 stopni",
"d": "180 stopni",
"prawidlowa_odpowiedz":"d"
}
]}
2
Answers
To show 2 questions from the file, just replace
with:
By the way,
Based on the file content you shared, it looks like the line
would need to be
for the code to work properly, but maybe you have this covered.
Also, as a general stylistic point, you don’t always need a counter index when iterating over lists.
is equivalent to
To avoid duplicate questions: