python - How to randomize the order of radio buttons in pyqt -


im making quiz , want radiobuttons in different positions. ive got working extent in random order in first question stay in order rest of quiz. want 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 load answer choices container, can use python's random (link) library 1 or both of following tools:

  • random.choice(seq) (link) makes random choice of possible items provided in seq
  • random.sample(pop, k) (link) choice k unique elements possible choices provided in pop, without replacing, think key in situation because don't want have same answer displayed twice on same question.

this simplify current if-statement block,

if randomnumber == 0:     layout.addwidget(self.correctanswer)     layout.addwidget(self.incorrectans1)     layout.addwidget(self.incorrectans2)     layout.addwidget(self.incorrectans3) 

to

choices = [self.correctanswer, self.incorrectans1, self.incorrectans2, self.incorrectans3] q in range(4):     layout.addwidget(random.sample(choices, 1)) 

also thought of cool idea. if have large "pool" of possible incorrect answers, can define function provide random selection of wrong answers. like:

def providewronganswers(self, number):     return random.sample(self.allwronganswerspool, number) 

which provide number incorrect answers add layout along correct.

in [7]: choices = range(1,11) in [9]: random.sample(choices, 3)  # returns different order of choices each time out[9]: [1, 7, 3] in [10]: random.sample(choices, 3) out[10]: [6, 9, 3] in [11]: random.sample(choices, 3) out[11]: [5, 4, 2] in [12]: random.sample(choices, 3) out[12]: [3, 6, 1] in [13]: random.sample(choices, 3) out[13]: [10, 8, 3] in [14]: random.sample(choices, 3) out[14]: [1, 7, 2] in [15]: random.sample(choices, 3) out[15]: [9, 7, 3] 

finally, random.shuffle(x) (link) method, takes sequence x , mixes them in place. combine random.sample(wrongchoices, 3) idea gives 3 random incorrect answers, add in correct answer , make sure correct answer isn't @ bottom, give em final stir random.shuffle(allanswerchoices)


Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -