im making a quiz and i want the radiobuttons to be in different positions. ive got it working to a extent as it would be in random order in the first question however it would then stay in that order for the rest of the quiz. I want it to randomize every time.
class MultipleChoice(QtGui.QWidget):
showTopicsSignal = pyqtSignal()
def __init__(self,parent=None):
super(MultipleChoice, self).__init__(parent)
self.initUI()
def initUI(self):
self.Questions=[]
self.Questionum = QLabel()
self.Questioninfo = QLabel()
self.Correctanswer = QRadioButton()
self.Incorrectans1 = QRadioButton()
self.Incorrectans2 = QRadioButton()
self.Incorrectans3 = QRadioButton()
self.Correctanswer.setAutoExclusive(True)
self.Incorrectans1.setAutoExclusive(True)
self.Incorrectans2.setAutoExclusive(True)
self.Incorrectans3.setAutoExclusive(True)
layout = QVBoxLayout(self)
layout.addWidget(self.Questionum)
layout.addWidget(self.Questioninfo)
randomnumber = randint(0,3)
if randomnumber == 0:
layout.addWidget(self.Correctanswer)
layout.addWidget(self.Incorrectans1)
layout.addWidget(self.Incorrectans2)
layout.addWidget(self.Incorrectans3)
elif randomnumber == 1:
layout.addWidget(self.Incorrectans1)
layout.addWidget(self.Correctanswer)
layout.addWidget(self.Incorrectans2)
layout.addWidget(self.Incorrectans3)
elif randomnumber == 2:
layout.addWidget(self.Incorrectans1)
layout.addWidget(self.Incorrectans2)
layout.addWidget(self.Correctanswer)
layout.addWidget(self.Incorrectans3)
elif randomnumber == 3:
layout.addWidget(self.Incorrectans1)
layout.addWidget(self.Incorrectans2)
layout.addWidget(self.Incorrectans3)
layout.addWidget(self.Correctanswer)
If you load your answer choices into a container, you can use Python's
random
(link) library with one or both of the following tools:random.choice(seq)
(link) which makes a random choice of the possible items provided inseq
random.sample(pop, k)
(link) which choicek
unique elements from the possible choices provided inpop
, without replacing, which I think is key in your situation because you don't want to have the same answer displayed twice on the same question.This would simplify your current if-statement block, from
to
Also just thought of another cool idea. If you have a large "pool" of possible incorrect answers, you can define a function specifically to provide a random selection of wrong answers. Something like:
which would provide you with
number
incorrect answers to add to the layout along with the correct.Finally,
random.shuffle(x)
(link) be another method, as it takes the sequencex
and mixes them up in place. So you could combine therandom.sample(wrongChoices, 3)
idea which gives you three random incorrect answers, then add in the correct answer and to make sure the correct answer isn't always at the bottom, give em a final stir withrandom.shuffle(allAnswerChoices)
The current structure of your seems over-complicated. It would be much simpler to randomize the question choices, rather than the widgets themselves.
Here's a simple demo that shows how that could be done: