oTree Forum >

Creating_Session Treatment

#1 by ebgnr (edited )

Hi Chris & others,

I am currently working on an experiment where I want the participants to work on a task for two rounds. In round 1, all particpants will work on the task without incentives and in round 2, participants will be randomly assigned to either an incentive treatment (pay-for-performance) or not (fixed pay). 

I have implemented sessions for that with creating_session, however, the assignment to treatment doesn't seem to work, as I get the error: TypeError: player.incentive_treatment is None. Also when I look into the recoded data, the incentive_treatment field is empty.

I have pasted my current code below. I am a newbie to oTree and appreciate any help and tips.

Thank you very much and best regards!

class Constants(BaseConstants):
    name_in_url = 'puzzle'
    players_per_group = None
    num_rounds = 2
    payment_per_correct_answer = 2
    payment_per_round = 1


class Subsession(BaseSubsession):
    pass

#Functions
@staticmethod
def creating_session(subsession:Subsession):
    session = subsession.session
    import random
    if session.round_number == 2:
        for player in subsession.get_players():
            player.participant.incentive_treatment = random.choice([True, False])
            print('Assigned Treatment is:')
            print(player.participant.incentive_treatment)

class Player(BasePlayer):
    round_number = models.IntegerField(blank=True)
    incentive_treatment = models.StringField(blank=True)
    ... Some other code ...
    
class Game(Page):
    ... Some other code ...

    @staticmethod
    def before_next_page(player: Player, timeout_happened):
        if player.round_number == 1:
            player.payoff = Constants.payment_per_round
        else:
            if player.round_number == 2:
                if player.participant.incentive_treatment==True:
                    player.payoff = Constants.payment_per_correct_answer * player.puzzles_solved
                else:
                    player.payoff = Constants.payment_per_round

Write a reply

Set forum username