from otree.api import * import random class C(BaseConstants): NAME_IN_URL = 'intro' PLAYERS_PER_GROUP = None NUM_ROUNDS = 1 ENDOWMENT = cu(10) class Subsession(BaseSubsession): pass class Group(BaseGroup): pass class Player(BasePlayer): computer_number = models.IntegerField( label="Computer number:", min=1 ) # CHOICE investment_choice = models.StringField( choices=[ ['A', 'Invest in Portfolio A'], ['B', 'Invest in Portfolio B'] ], widget=widgets.RadioSelect ) payoff_result = models.CurrencyField() # P6 DEMOGRAPHICS age = models.IntegerField(min=18, max=99, label="Age") sex = models.StringField( choices=[ ['Male', 'Male'], ['Female', 'Female'], ['prefer_not_to_say', 'Prefer not to say'] ], widget=widgets.RadioSelect ) # EMPLOYMENT STATUS employment_status = models.StringField( choices=[ ['full_time', 'Working full-time'], ['part_time', 'Working part-time'], ['self_employed', 'Self-employed'], ['student', 'Student'], ['not_working', 'Not working'], ['retired', 'Retired'], ['other', 'Other'], ], widget=widgets.RadioSelect, label="What is your job status?" ) employment_other = models.StringField( blank=True, label="If Other, please specify:" ) # INVESTMENT EXPERIENCE invested_before = models.StringField( choices=[['yes', 'Yes'], ['no', 'No']], widget=widgets.RadioSelect, label="Have you invested money before?" ) investing_experience = models.StringField( choices=[ ['none', 'None'], ['little', 'A little'], ['some', 'Some'], ['a_lot', 'A lot'], ], widget=widgets.RadioSelect, label="How much investing experience do you have?" ) invested_in = models.LongStringField( blank=True, label="What have you invested in before?" ) invested_in_other = models.StringField( blank=True, label="If Other, please specify:" ) # P7 SKILLS skill_1 = models.IntegerField(choices=[1, 2, 3, 4, 5, 6], widget=widgets.RadioSelectHorizontal) skill_2 = models.IntegerField(choices=[1, 2, 3, 4, 5, 6], widget=widgets.RadioSelectHorizontal) skill_3 = models.IntegerField(choices=[1, 2, 3, 4, 5, 6], widget=widgets.RadioSelectHorizontal) skill_4 = models.IntegerField(choices=[1, 2, 3, 4, 5, 6], widget=widgets.RadioSelectHorizontal) skill_5 = models.IntegerField(choices=[1, 2, 3, 4, 5, 6], widget=widgets.RadioSelectHorizontal) skill_6 = models.IntegerField(choices=[1, 2, 3, 4, 5, 6], widget=widgets.RadioSelectHorizontal) # P8 RESPONSIBILITY responsibility = models.IntegerField(choices=[1, 2, 3, 4, 5, 6], widget=widgets.RadioSelectHorizontal) # P9 TRUST trust_1 = models.IntegerField(choices=[1, 2, 3, 4, 5, 6], widget=widgets.RadioSelectHorizontal) trust_2 = models.IntegerField(choices=[1, 2, 3, 4, 5, 6], widget=widgets.RadioSelectHorizontal) trust_3 = models.IntegerField(choices=[1, 2, 3, 4, 5, 6], widget=widgets.RadioSelectHorizontal) trust_4 = models.IntegerField(choices=[1, 2, 3, 4, 5, 6], widget=widgets.RadioSelectHorizontal) trust_5 = models.IntegerField(choices=[1, 2, 3, 4, 5, 6], widget=widgets.RadioSelectHorizontal) trust_6 = models.IntegerField(choices=[1, 2, 3, 4, 5, 6], widget=widgets.RadioSelectHorizontal) comfort_1 = models.IntegerField( choices=[1, 2, 3, 4, 5, 6], widget=widgets.RadioSelectHorizontal ) comfort_2 = models.IntegerField( choices=[1, 2, 3, 4, 5, 6], widget=widgets.RadioSelectHorizontal ) comfort_3 = models.IntegerField( choices=[1, 2, 3, 4, 5, 6], widget=widgets.RadioSelectHorizontal ) # ---------------------------- # P10 UNDERSTANDING (3 items) # ---------------------------- understand_1 = models.IntegerField( choices=[1, 2, 3, 4, 5, 6], widget=widgets.RadioSelectHorizontal ) understand_2 = models.IntegerField( choices=[1, 2, 3, 4, 5, 6], widget=widgets.RadioSelectHorizontal ) understand_3 = models.IntegerField( choices=[1, 2, 3, 4, 5, 6], widget=widgets.RadioSelectHorizontal ) def set_payoff(self): if not self.investment_choice: return payoff_counts = self.session.vars.get('payoff_counts') if payoff_counts is None: payoff_counts = { 'A_high': 0, 'A_low': 0, 'B_high': 0, 'B_low': 0, } if self.investment_choice == 'A': total_a = payoff_counts['A_high'] + payoff_counts['A_low'] + 1 target_a_high = round(total_a * 0.5) target_a_low = total_a - target_a_high need_high = target_a_high - payoff_counts['A_high'] need_low = target_a_low - payoff_counts['A_low'] if need_high > need_low: self.payoff_result = cu(17.50) payoff_counts['A_high'] += 1 elif need_low > need_high: self.payoff_result = cu(7.50) payoff_counts['A_low'] += 1 else: if random.random() < 0.5: self.payoff_result = cu(17.50) payoff_counts['A_high'] += 1 else: self.payoff_result = cu(7.50) payoff_counts['A_low'] += 1 elif self.investment_choice == 'B': total_b = payoff_counts['B_high'] + payoff_counts['B_low'] + 1 target_b_high = round(total_b * 0.75) target_b_low = total_b - target_b_high need_high = target_b_high - payoff_counts['B_high'] need_low = target_b_low - payoff_counts['B_low'] if need_high > need_low: self.payoff_result = cu(13.50) payoff_counts['B_high'] += 1 elif need_low > need_high: self.payoff_result = cu(9.50) payoff_counts['B_low'] += 1 else: if random.random() < 0.75: self.payoff_result = cu(13.50) payoff_counts['B_high'] += 1 else: self.payoff_result = cu(9.50) payoff_counts['B_low'] += 1 self.session.vars['payoff_counts'] = payoff_counts