#πŸ”’ Need help retrieving questions inside of a dictionary at random

14 messages Β· Page 1 of 1 (latest)

white shore
#

Hello, I am making a practice quiz maker/taker. Right now I want to have my program prompt the user to input what Subject and chapter they want to study. Then the program will display the question first and wait for an input then display the answer afterwards. My values is a list of tuples but I want to retrieve the questions from that tuple. Basically the first item of the list that is inside the tuple.

For context print(pracQA_Data[userInputSubject].values()) would print something like this.

dict_values([[['question1', 'answer1'], ['question2', 'answer2']], [['question3', 'answer3']]])

I want my program to randomly pick the questions from this list of tuples on by one.
In doing some research I found that random.choice works with dictionaries but I don't know how to progress or use it in this case.

silent viperBOT
#

@white shore

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

white shore
#

This is what I have if you need further context

def quizTaker():
    with open("practiceQuiz.json", "r") as json_QAfile:
        pracQA_Data = json.load(json_QAfile)
        print("What subject would you like to study?")
        print(", ".join(pracQA_Data.keys()))
        userInputSubject = input()
        if userInputSubject in pracQA_Data.keys():
            print(",".join(pracQA_Data[userInputSubject].keys()))
            print("What chapter would you like to study (or type random)")
            userInputChapter = input()
            if userInputChapter == "random":
             
            ```
pastel steeple
#

Can you just shuffle the QA pairs and then iterate through that list?

white shore
#

Is there a method to shuffle them or do I have to manually do it myself? I wanted to make it dynamic. So should I have a variable that holds the dict values?

pastel steeple
#

random.shuffle

pastel steeple
white shore
#

Well the list is already made and is loaded in a json file. I have to build another list?

pastel steeple
#

Yes because you want to then shuffle it in case the user has selected random right?

white shore
#

Yeah

#

I will try to do that now

#

So I would do
new_list = list(pracQA_Data[userInputSubject].values()) ?

silent viperBOT
#
Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.