oTree Forum >

Removing a element on a list and pass the new list to the next round for each participant

#1 by giodini121

Hello all, 

I am trying to solve this but I cannot make it. 
From round 2 I need to assign one random product from a list to each participant. The product is the result of a random draw and it can be different from participant to participant. Then I need to remove this product drawn from the list of available products and reiterate the process for every round onward. Every round each player should have a product extracted from the list of available products which shrinks as rounds go on.

Can someone help me?

    import random

    for player in subsession.get_players():
        participant = player.participant

        if subsession.round_number == 1:
            product = random.choice(['Chocolate'])
        else:
            round_number = subsession.round_number
            all_products = ['Coffee', 'Dishwashing', 'Buds', 'Avocado']
            removed_products = participant.vars.get('removed_products', [])

            # Create a list of available products excluding removed products
            available_products = [x for x in all_products if x not in removed_products]

            if available_products:
                product = random.choice(available_products)
                removed_products.append(product)
                participant.vars['removed_products'] = removed_products
            else:
                product = 'No product available'
            # Assign the product for this round to the player
        player.product_this_round = product
        
  AND in settings.py I have:
  PARTICIPANT_FIELDS = ['removed_products']

#2 by BonnEconLab

I have copied-and-pasted your code, and it seems to be working perfectly fine. More specifically, I have put your for loop in

def creating_session(subsession):

That is,

def creating_session(subsession):

    all_products = ['Coffee', 'Dishwashing', 'Buds', 'Avocado']
    for player in subsession.get_players():
        participant = player.participant
        if subsession.round_number == 1:
            product = 'Chocolate'
            available_products = all_products
        else:
            round_number = subsession.round_number
            removed_products = participant.vars.get('removed_products', [])
            # Create a list of available products excluding removed products
            available_products = [x for x in all_products if x not in removed_products]
            if available_products:
                product = random.choice(available_products)
                removed_products.append(product)
                participant.vars['removed_products'] = removed_products
            else:
                product = 'No product available'
        # Assign the product for this round to the player
        player.product_this_round = product
        player.available_products = str(available_products)  # For debugging only
        player.all_products = str(all_products)  # For debugging only

What did you actually mean by, “I am trying to solve this but I cannot make it”? You didn’t specify which kind of error you’re facing.

#3 by giodini121

I think you solved it by rearranging some elements. What was not working was the random draw without replacement for each individual. Each player could have the same product one round after the other. But now I uploaded the code you sent and it works as I wanted. 

Thank you very much!

Write a reply

Set forum username