from otree.api import * c = cu doc = '' class C(BaseConstants): PLAYERS_PER_GROUP = None NUM_ROUNDS = 1 PROBABILITY = 50 RESULT = True NAME_IN_URL = 'SCL' class Subsession(BaseSubsession): pass def creating_session(subsession: Subsession): session = subsession.session for p in subsession.get_players(): n = 5 # create list of lottery indices # -------------------------------------------------------------------------------------------------------- indices = [j for j in range(1, n + 1)] # create list of low and high outcomes (matched by index) # -------------------------------------------------------------------------------------------------------- outcomes_lo = [cu(500), cu(400), cu(300), cu(200), cu(100)] outcomes_hi = [cu(500), cu(700), cu(900), cu(1100), cu(1300)] # create list of lotteries # -------------------------------------------------------------------------------------------------------- p.participant.vars['scl_lotteries'] = list(zip(indices, outcomes_lo, outcomes_hi)) class Group(BaseGroup): pass class Player(BasePlayer): lottery_choice = models.IntegerField() outcome_to_pay = models.StringField() outcome_lo = models.CurrencyField() outcome_hi = models.CurrencyField() continuenexttask = models.BooleanField(choices=[[True, 'Yes'], [False, 'No']], label='continue', widget=widgets.RadioSelect) Student_Major = models.StringField(label='What is your major?') Student_Race = models.IntegerField(label='What is your race') Student_Age = models.IntegerField(label='What is your age?') Student_Gender = models.StringField(choices=[['Male', 'Male'], ['Female', 'Female']], label='What is your gender') Student_Education = models.StringField(choices=[['Master', 'Master'], ['PhD', 'PhD'], ['Fresh', 'Fresh'], ['Sophomore', 'Sophomore'], ['Junior', 'Junior'], ['Senior', 'Senior']], label='What is your highest eduction level') Farmer = models.BooleanField(choices=[[True, 'Yes'], [False, 'No']], label='Have you/your family owned farm before?') def set_payoffs(player: Player): participant = player.participant import random # choose outcome to pay (high or low) dependent on # ------------------------------------------------------------------------------------------------------------ p = C.PROBABILITY rnd = random.randint(1, 100) player.outcome_to_pay = "B" if rnd <= p else "A" # select lottery choice out of list of lotteries # ------------------------------------------------------------------------------------------------------------ lottery_selected = [ i for i in player.participant.vars['scl_lotteries'] if i[0] == player.lottery_choice ] lottery_selected = lottery_selected[0] # store payoffs of chosen lottery in the model # ------------------------------------------------------------------------------------------------------------ player.outcome_lo = lottery_selected[1] player.outcome_hi = lottery_selected[2] # set player's payoff # ------------------------------------------------------------------------------------------------------------ if player.outcome_to_pay == "B": player.payoff = player.outcome_hi else: player.payoff = player.outcome_lo # set global variables # ------------------------------------------------------------------------------------------------------------ player.participant.vars['scl_payoff'] = player.payoff player.participant.vars['lottery_choice'] = player.lottery_choice player.participant.vars['outcome_lo'] = player.outcome_lo player.participant.vars['outcome_hi'] = player.outcome_hi player.participant.vars['outcome_to_pay'] = player.outcome_to_pay player.participant.vars['Total']=500+player.payoff participant.Total1=800 participant.temp_total=participant.Total participant.temp_total1=participant.Total1 participant.Task1=player.payoff participant.Task2=0 participant.trial=0 class General_Instruction(Page): form_model = 'player' form_fields = ['Student_Major', 'Student_Age', 'Student_Gender', 'Student_Race', 'Student_Education', 'Farmer'] class Task1_instruction(Page): form_model = 'player' class Decision(Page): form_model = 'player' form_fields = ['lottery_choice'] timeout_seconds = 60 @staticmethod def vars_for_template(player: Player): participant = player.participant return {'lotteries': player.participant.vars['scl_lotteries']} @staticmethod def before_next_page(player: Player, timeout_happened): set_payoffs(player) class Results(Page): form_model = 'player' @staticmethod def vars_for_template(player: Player): participant = player.participant lottery = (player.lottery_choice, player.outcome_lo, player.outcome_hi) return { 'lottery_choice': player.lottery_choice, 'outcome_to_pay': player.outcome_to_pay, 'index': player.participant.vars['scl_lotteries'].index(lottery) + 1, 'hi_lo': "B" if player.outcome_to_pay == "high" else "A", 'lottery': [lottery], 'prob_hi': "{0:.1f}".format(C.PROBABILITY) + "%", 'prob_lo': "{0:.1f}".format(100 - C.PROBABILITY) + "%", } class End(Page): form_model = 'player' class Task_2_Instruction(Page): form_model = 'player' class Trial(Page): form_model = 'player' class Waitfortrial(WaitPage): wait_for_all_groups = True title_text = 'Wait for quiz to start' page_sequence = [General_Instruction, Task1_instruction, Decision, Results, End, Task_2_Instruction, Trial, Waitfortrial]