from otree.api import * c = cu doc = 'The game provides a loss at the start for players over the 4 rounds' class Constants(BaseConstants): name_in_url = 'Experiment_3_Loss_In_The_Beginning' players_per_group = None num_rounds = 3 MF_1_choice = 'Mutual Fund 1' MF_2_choice = 'Mutual Fund 2' NTK_choice = 'I would need more information' Default_choice = 'Not attempted by player' returns_round_1 = (-4.3, -4.1) returns_round_2 = (2.1, 2.3) returns_round_3 = (2.3, 2.5) returns_round_4 = (4.1, 4.3) class Subsession(BaseSubsession): return_mf1 = models.FloatField() return_mf2 = models.FloatField() class Group(BaseGroup): pass def returns_round(player): import random return_for_round = random.uniform(getattr(Constants, "returns_round_%s" % player.round_number)[0], getattr(Constants, "returns_round_%s" % player.round_number)[1]) setattr(player, "return_for_round",return_for_round) return return_for_round def player_final_choice(player): if any((l==Constants.MF_1_choice for l in [player.choices_1,player.choices_2,player.choices_3,player.choices_4])): setattr(player, "final_choice", Constants.MF_1_choice) return Constants.MF_1_choice if any((l==Constants.MF_2_choice for l in [player.choices_1,player.choices_2,player.choices_3,player.choices_4])): setattr(player, "final_choice", Constants.MF_2_choice) return Constants.MF_2_choice def decision_summary(player): decisions = [p.final_choice for p in player.in_previous_rounds()] decisions = decisions + [Constants.Default_choice]*(Constants.num_rounds - len(decisions)) return decisions def total_return(player): player_info = player.in_previous_rounds() + [player] return sum([p.return_for_round for p in player_info]) if player_info else 0 class Player(BasePlayer): Age = models.IntegerField(label='What is your age', max=125, min=13) Gender = models.StringField(choices=[['Male', 'Male'], ['Female', 'Female'], ['Non-binary', 'Non-binary'], ['Prefer not to say', 'Prefer not to say']], label='What is your gender?', widget=widgets.RadioSelect) Name = models.StringField() Email = models.LongStringField() choices_1 = models.StringField(choices=[['Mutual Fund 1', 'Mutual Fund 1'], ['Mutual Fund 2', 'Mutual Fund 2'], ['I would need more information', 'I would need more information']], initial='Not attempted by player', widget=widgets.RadioSelect) choices_2 = models.StringField(choices=[['Mutual Fund 1', 'Mutual Fund 1'], ['Mutual Fund 2', 'Mutual Fund 2'], ['I would need more information', 'I would need more information']], initial='Not attempted by player', widget=widgets.RadioSelect) choices_3 = models.StringField(choices=[['Mutual Fund 1', 'Mutual Fund 1'], ['Mutual Fund 2', 'Mutual Fund 2'], ['I would need more information', 'I would need more information']], initial='Not attempted by player', widget=widgets.RadioSelect) choices_4 = models.StringField(choices=[['Mutual Fund 1', 'Mutual Fund 1'], ['Mutual Fund 2', 'Mutual Fund 2']], initial='Not attempted by player', widget=widgets.RadioSelect) return_for_round = models.FloatField(initial=0) final_choice = models.StringField() class Demographics(Page): form_model = 'player' form_fields = ['Name', 'Age', 'Email', 'Gender'] @staticmethod def is_displayed(player): return True if player.round_number==1 else False class First_Page(Page): form_model = 'player' @staticmethod def is_displayed(player): return True if player.round_number == 1 else False class Section_1(Page): form_model = 'player' form_fields = ['choices_1'] class Section_2(Page): form_model = 'player' form_fields = ['choices_2'] @staticmethod def is_displayed(player): return True if player.choices_1 == Constants.NTK_choice else False class Section_3(Page): form_model = 'player' form_fields = ['choices_3'] @staticmethod def is_displayed(player): return True if player.choices_2 == Constants.NTK_choice else False class Section_4(Page): form_model = 'player' form_fields = ['choices_4'] @staticmethod def is_displayed(player): return True if player.choices_3 == Constants.NTK_choice else False class Results(Page): form_model = 'player' @staticmethod def is_displayed(player): return False if all((l==Constants.Default_choice for l in [player.choices_1,player.choices_2,player.choices_3,player.choices_4])) else True @staticmethod def vars_for_template(player): return dict(final_choice=player_final_choice(player), return_from_mf=returns_round(player)) class Decision_summary(Page): form_model = 'player' @staticmethod def vars_for_template(player): player_in_all_rounds = player.in_previous_rounds() + [player] return dict(total_returns=total_return(player), past_decisions = player_in_all_rounds) page_sequence = [Demographics, First_Page, Section_1, Section_2, Section_3, Section_4, Results, Decision_summary]