Aide pour fonction aléatoire

Résolu/Fermé
Clementine1998 Messages postés 34 Date d'inscription dimanche 7 février 2016 Statut Membre Dernière intervention 1 juin 2016 - 11 avril 2016 à 13:38
Clementine1998 Messages postés 34 Date d'inscription dimanche 7 février 2016 Statut Membre Dernière intervention 1 juin 2016 - 14 avril 2016 à 10:52
Bonjour, j'ai crée un QCM où j'ai réussi à mettre les questions aléatoirement. Le problème c'est que les questions peuvent se répéter deux fois. Est-ce que quelqu'un peut m'aider?

from tkinter import *
import random

fen=Tk()

Consigne=Label(fen,text="Cocher la bonne reponse")
Consigne.pack()

#Questions Globales # la repetition correspond a la bonne reponse
ListeDeQuestionsReponses = [["Combien de couleurs sur le drapeau Francais?","3","2","4"],
["De quelle couleur est le rond sur le drapeau Chine","Rouge","Bleu","Vert"],
["Combien de doigts sur une main?","5","2","3"]]

x = len(ListeDeQuestionsReponses)

for i in range(x) :
QuestionAuHasard=random.choice(ListeDeQuestionsReponses)#On prend une question #Qu'on enleve de notre liste de questions
Qx=[QuestionAuHasard]
len(Qx)
Q0 = Qx[0][0]
R1 = Qx[0][1]
R2 = Qx[0][2]
R3 = Qx[0][3]
Question = Label(fen, text = Q0)
Question.pack()
Reponse1 = Label(fen, text = R1)
Reponse1.pack()
Reponse2 = Label(fen, text = R2)
Reponse2.pack()
Reponse3 = Label(fen, text = R3)
Reponse3.pack()

fen.mainloop()
A voir également:

2 réponses

Clementine1998 Messages postés 34 Date d'inscription dimanche 7 février 2016 Statut Membre Dernière intervention 1 juin 2016
11 avril 2016 à 18:45
J'ai finalement réussi. Voici mon code:

from tkinter import *
import random

fen=Tk()

Consigne=Label(fen,text="Cocher la bonne reponse")
Consigne.pack()
AllerAlaligne=Label(fen, text = "")
AllerAlaligne.pack()

Question = [["Combien de couleurs sur le drapeau Francais?","3","2","4"],
["De quelle couleur est le rond sur le drapeau Chine","Rouge","Bleu","Vert"],
["Combien de doigts sur une main?","5","2","3"]]

x = len(Question)

for i in range(x) :
QuestionAuHasard=random.choice(Question)
Qx=[QuestionAuHasard]
len(Qx)
Q0 = Qx[0][0]
R1 = Qx[0][1]
R2 = Qx[0][2]
R3 = Qx[0][3]
quest=Label(fen,text= Q0 )
quest.pack()
rep1=Label(fen, text= R1)
rep2=Label(fen, text= R2)
rep3=Label(fen, text= R3)
rep1.pack()
rep2.pack()
rep3.pack()
AllerAlaligne=Label(fen, text = "")
AllerAlaligne.pack()
PlaceNombre=Question.index(QuestionAuHasard)
Place=int(PlaceNombre)
del(Question[Place])


fen.mainloop()


J'aimerais maintenant que les questions s'affichent aléatoirement. Est-ce que quelqu'un peu m'aider?
0
jisisv Messages postés 3645 Date d'inscription dimanche 18 mars 2001 Statut Modérateur Dernière intervention 15 janvier 2017 934
12 avril 2016 à 03:31
Utilise le module random et par exempple shuffle
Python 2.7.11+ (default, Mar 30 2016, 21:00:42) 
[GCC 5.3.1 20160323] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import random
>>> anArray = range(1, 11)
>>> random.shuffle(anArray)
>>> anArray
[1, 3, 7, 8, 9, 4, 10, 2, 6, 5]

Voir random — Generate pseudo-random number

0
Clementine1998 Messages postés 34 Date d'inscription dimanche 7 février 2016 Statut Membre Dernière intervention 1 juin 2016
12 avril 2016 à 14:15
Je comprend pas pourquoi les réponses ne s'affichent pas quand on lance le programme

from tkinter import *
import random

fen=Tk()

Consigne=Label(fen,text="Cocher la bonne reponse")
Consigne.pack()

AllerAlaligne=Label(fen, text = "") #juste pour la présentation
AllerAlaligne.pack()


ListeDeQuestionsReponses = [["Combien de couleurs sur le drapeau Francais?","3","2"],
["De quelle couleur est le rond sur le drapeau Chine","Rouge","Bleu"],
["Combien de doigts sur une main?","5","2"]]

x = len(ListeDeQuestionsReponses)

for i in range(x) :
QuestionAuHasard=random.choice(ListeDeQuestionsReponses)
Qx=[QuestionAuHasard]
len(Qx)
Q0 = Qx[0][0]
R1 = Qx[0][1]
R2 = Qx[0][2]
Question=Label(fen,text = Q0 )
Question.pack()

ListeDeReponse=[R1,R2]
y=len(ListeDeReponse)
for j in range (y) :
ReponseAuHasard=random.shuffle(ListeDeReponse)
Reponse=Label(fen,text=ReponseAuHasard)
Reponse.pack()


AllerAlaligne=Label(fen, text = "")
AllerAlaligne.pack()

PlaceNombre=ListeDeQuestionsReponses.index(QuestionAuHasard)
Place=int(PlaceNombre)
del(ListeDeQuestionsReponses[Place])

fen.mainloop()
0
Clementine1998 Messages postés 34 Date d'inscription dimanche 7 février 2016 Statut Membre Dernière intervention 1 juin 2016 > Clementine1998 Messages postés 34 Date d'inscription dimanche 7 février 2016 Statut Membre Dernière intervention 1 juin 2016
14 avril 2016 à 10:52
Je précise aussi que je compte mettre des Checkbutton devant les réponses et un bouton valider qui dira si la réponse est bonne ou non. C'est compatible avec la fonction random.shuffle?
0