Home > Software design >  Random data from json file
Random data from json file

Time:01-01

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:\Users\Tymek\PycharmProjects\pythonProject1\quiz!\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"
                }
              ]}

CodePudding user response:

To show 2 questions from the file, just replace

for i in range(0, len(questions)):
    show_questions(questions[i])

with:

random_questions = random.sample(questions, k=2)
for i in range(0, len(random_questions)):
    show_questions(questions[i])

By the way,

Based on the file content you shared, it looks like the line

questions = json.load(file)

would need to be

questions = json.load(file)['questions']

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.

for i in range(0, len(questions)):
    show_questions(questions[i])

is equivalent to

for question in questions:
    show_questions(question)

CodePudding user response:

To avoid duplicate questions:

import random
question_pool = [
                {
                  "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"
                }
              ]
selected_questions = []
while len(selected_questions) < 2:
    selected = random.randrange(len(question_pool))
    selected_questions.append(question_pool[selected])
    del question_pool[selected]
print(selected_questions)
  • Related