samedi 16 février 2019

selecting Models Randomly under specific conditions

I am completely biggener. I've made a WebApp using django to organize a class room. I want to generate random work groups of 2 students where the two students don't talk the same first_languge. Evrey thing Works, but still have results don't match that condition.

this is the code that I have done:

from students.models import students
import random

#make a list of all students 
 a =list(students.objects.all())

#the select function
 def group (x,y):
    res = []
    for i in range(y):
        while len(res)<y:
             res = random.sample(x, k=y)
             while x[i].first_language == x[i-1].first_language:
                 group(x,y)
                 return None
     return res


#multiple call for the function
 for n in range(10):
     p = group(a,2)
     print(p)

if i call the function multiple times i get groups from students with the same mother language, and this is wrong.

Aucun commentaire:

Enregistrer un commentaire