Is there any way to be able to randomize the order the questions are outputted to the User?

Viewed 15

I am just stuck on finding a solution to randomize the order the questions are outputted. I am somewhat a beginner and this is from a tutorial that I am trying to complete and change for my own version of a Quiz Game. If anyone could help it would be greatly appreciated.

QUESTIONS = {
        "Which keyword do you use to loop over a given list of elements": [
            "for", "while", "each", "loop"
        ],
        "What's the purpose of the built-in zip() function": [
            "To iterate over two or more sequences at the same time",
            "To combine several strings into one",
            "To compress several files into one archive",
            "To get information from the user",
        ],
        "What's the name of Python's sorting algorithm": [
            "Timsort", "Quicksort", "Merge sort", "Bubble sort"
        ],
    }
    
    for question, alternatives in QUESTIONS.items():
        correct_answer = alternatives[0]
        sorted_alternatives = sorted(alternatives)
        for label, alternative in enumerate(sorted_alternatives):
            print(f"  {label}) {alternative}")
    
        answer_label = int(input(f"{question}? "))
        answer = sorted_alternatives[answer_label]
        if answer == correct_answer:
            print("Correct!")
        else:
            print(f"The answer is {correct_answer!r}, not {answer!r}")
0 Answers
Related