oTree Forum >

Team assignment based on number entered

#1 by caroooo

Hello,

we want to create teams of three based on a number the players have first entered. So in short, this is what happens:

1. Players move a slider from 0 to 500 and then enter the number they land on. (This code is complete)

2. The numbers are sorted from smallest to largest and then the three players who have entered the lowest three numbers are assigned to team 1, the next three numbers and the players who chose them are team 2 and so on. 

We are stuck on the second step. Does anyone have any idea how this could be done? Any advice is highly appreciated!

Greetings
Caro

#2 by xindamate_xyz

Hi, 

you mention team 1 or team 2, is it means a Group?

assume that, there are 9 players and each choose a number. the code may like this 

```
from otree.api import Page, WaitPage
from .models import Constants

class ChooseNumber(Page):
    form_model = 'player'
    form_fields = ['number']

class ResultsWaitPage(WaitPage):
    after_all_players_arrive = 'set_payoffs'

class DisplaySortedNumbers(Page):
    def vars_for_template(player):
        # get all numbers and sorted
        numbers = sorted([p.number for p in player.group.get_players()])
        # divided to groups
        group_size = len(numbers) // 3
        groups = [numbers[i:i + group_size] for i in range(0, len(numbers), group_size)]
        return {
            'sorted_numbers': numbers,
            'groups': groups
        }

page_sequence = [ChooseNumber, ResultsWaitPage, DisplaySortedNumbers]
```

Thanks,
Tina https://otree.xindamate.xyz/en/

#3 by xindamate_xyz (edited )

Hi,

I've created a demo project for you. Here's the demo link: http://42.193.99.75:8008/demo.
and here is the Github code Repo https://github.com/The-Three-Fish/two-app

In the demo, "app1" allows you to choose a number, and "app2" uses the value to divide it into 3 groups. You can test both by selecting "app1app2" to see if the effects meet your expectations.

Thanks,
Tina https://otree.xindamate.xyz/en/

#4 by caroooo

Hi Tina,

this is amazing! Thank you so much for this.

We appreciate your help and all your effort!

Greetings,
Caro

Write a reply

Set forum username