[Solved] how to randomize order of questions in a quiz in python? [closed]


Well, random.randint() returns a list with integers put in a random number. What you really need is random.shuffle(). So you should make a list (I’ll call it questions) because random.shuffle only works when there is a list in the parentheses. This should work because all you need to do is put your questions in the list, and let random.shuffle() do its magic:

questions = ['Question 1', 'Question 2', 'Question 3'] #You can add as many questions as you like
random.shuffle(questions)  #Mixes the items in "questions" into a random order
print questions[0]
print questions[1]
print questions[2]

And there are many different combinations/results you can get using random.shuffle() in this way. To also have the answers, same idea, except you need a while loop and know the order of the questions so you can choose the correct answer choices for each question. Still adding random.shuffle() for the answers:

questions = ['Question 1', 'Question 2', 'Question 3']
originals = [['Question 1', 'a1'], ['Question 2', 'b1'], ['Question 3', 'c1']]
answers = [['a1'], ['a2'], ['a3']], [['b1'], ['b2'], ['b3']], [['c1'], ['c2'], ['c3']]        #List of answers for each question
selected_answers = [] #Contains selected answers
random.shuffle(questions)
random.shuffle(answers[0])
random.shuffle(answers[1])
random.shuffle(answers[2])
question = 0
while question < 4:
    if questions[0] == 'Question 1':
        print 'Question 1'
        print answers[0][0], answers[0][1], answers[0][2]
        chosen = raw_input('Enter 1 for the first answer, 2 for the second answer and 3 for the third one.')
        selected_answers.append(chosen)
        del questions[0]
        question += 1
    elif questions[0] == 'Question 2':
        print 'Question 2'
        print answers[1][0], answers[1][1], answers[1][2]
        chosen = raw_input('Enter 1 for the first answer, 2 for the second answer and 3 for the third one.')
        selected_answers.append(chosen)
        del questions[0]
        question += 1
    elif questions[0] == 'Question 3':
        print 'Question 3'
        print answers[2][0], answers[2][1], answers[2][2]
        chosen = raw_input('Enter 1 for the first answer, 2 for the second answer and 3 for the third one.')
        selected_answers.append(chosen)
        del questions[0]
        question += 1

Using originals, you can check the answers from selected_answers with the correct one with its corresponding question. How you do that is your choice. This should be a base to help you.

4

solved how to randomize order of questions in a quiz in python? [closed]