oTree Forum >

error with group formation when sorting participants

#1 by GergelyH

Hi All,

I would like to form groups based on risk aversion. Participants fill out a risk aversion preference elicitation in the first app of the experiment. Then start the second app with a waitpage, I use the group_by_arrival_time method in which I want to sort participants by the risk aversion measure and form groups based on that. I gather C.MATCHING_GROUPSIZE number of persons and split them into groups of C.GROUPSIZE. I store the risk aversion measure as a participant variable. 

The code is:
if len(waiting_players) >= C.MATCHING_GROUPSIZE:
        sorted_players = sorted(waiting_players, key=lambda p: p.participant.riskaversion)
        
        groupnum = int(C.MATCHING_GROUPSIZE/C.GROUPSIZE)
        groups = []
        
        for i in range(groupnum):
            start_idx = i * C.GROUPSIZE
            end_idx = start_idx + C.GROUPSIZE
            group = sorted_players[start_idx:end_idx]
            groups.append(group)
        
        return groups
        
However, I get an error AttributeError: 'list' object has no attribute 'participant' when there are enough people in the waitlist and groups would be formed. I attach a screenshot. This is weird since the players in the waitlist should have the participant characteristic. Any idea what's going on?

Best,
Gergely

#2 by GergelyH

I realized that the problem is that groups is a list of lists, therefore an element has no attribute participant. Can this method return multiple groups at once? if not how to go around the problem?

#3 by Daniel_Frey

Hi Gergely

If I understand you correctly, you wait for all players to arrive at this page, correct?

If yes, you could set the group matrix according to your needs: https://otree.readthedocs.io/en/latest/multiplayer/groups.html#set-group-matrix

This allows you to return multiple groups at once.

Write a reply

Set forum username