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

2024/11/18 12:26:35

My coursework is about a game. So far I have programmed registration but on the second task it says to generate questions for a quiz, in a randomized order.

I have managed to make questions and the answers but, I don't know how to make them come up in a different order each time a new user plays. I have tried using the random.randint() code but I don't think I am using it correctly.

Answer

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 += 1elif 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 += 1elif 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.

https://en.xdnf.cn/q/120073.html

Related Q&A

How transform days to hours, minutes and seconds in Python

I have value 1 day, 14:44:00 which I would like transform into this: 38:44:00. Ive tried the following code: myTime = ((myTime.days*24+myTime.hours), myTime.minutes, myTime.seconds) But it doesnt work.…

Brute Force in python [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 10 years ago.Improv…

Choosing only non-zeros from a long list of numbers in text file

I have a text file with a long list of numbers. I would like to choose only the non-zeros and make another text file. This is a portion of the input file:0.00000E+00 0.00000E+00 0.00000E+00 0.00000…

Why can I not plot using Python on repl.it

For practical reasons, I want to test a small piece of Pyton code on repl.it (webbased, so I do not need to install Python).The codeimport numpy as np import matplotlib.pyplot as plttime = np.array([0,…

Pull Data from web link to Dataframe [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 5…

Python program that rolls a fair die counts the number of rolls before a 6 shows up

import randomsample_size = int(input("Enter the number of times you want me to roll the die: "))if (sample_size <=0):print("Please enter a positive number!")else:counter1 = 0coun…

How to read CSV file in Python? [duplicate]

This question already has answers here:How do I read and write CSV files?(9 answers)Closed 1 year ago.Im using Spyder for Python 2.7 on Windows 8. Im trying to open and read a csv file and see all the…

How to match the bundle id for android app?

Id like to match the urls like this:input: x = "https://play.google.com/store/apps/details?id=com.alibaba.aliexpresshd&hl=en"get_id(x)output: com.alibaba.aliexpresshdWhat is the best way…

Memory Usage During running a Deep learning CNN Model in Colab

I am conducting a research which requires me to know the memory used during run time by the model when i run a deep learning model(CNN) in google colab. Is there any code i can use to know the same .Ba…

Gensim example, TypeError:between str and int error

When running the below code. this Python 3.6, latest Gensim library in Jupyterfor model in models:print(str(model))pprint(model.docvecs.most_similar(positive=["Machine learning"], topn=20))[1…