import random from otree.api import * doc = """ Your choice """ class C(BaseConstants): NAME_IN_URL = 'choice' PLAYERS_PER_GROUP = None NUM_ROUNDS = 1 BONUS = 5 CM100 = __name__ + '/CM100.html' DBM100 = __name__ + '/DBM100.html' class Subsession(BaseSubsession): pass class Group(BaseGroup): pass class Player(BasePlayer): choice = models.StringField() CM = models.IntegerField() DBM_left = models.IntegerField() DBM_right = models.IntegerField() DBM_list = models.LongStringField() random_num = models.IntegerField() preference = models.IntegerField( choices=[0,1,2,3,4,5,6,7,8,9,10], label="strength of preference", widget=widgets.RadioSelectHorizontal ) knowledge = models.StringField( # label="Do you consider yourself familiar with probability theory and statistics?", choices=[ ['A', 'A. Yes'], ['B', 'B. No'], ], ) estimate = models.StringField( #label="Do you consider yourself good at estimating things without counting?", choices=[ ['A', 'A. Yes'], ['B', 'B. No'], ], ) feedback1 = models.LongStringField(blank=True) feedback2 = models.LongStringField(blank=True) feedback3 = models.LongStringField(blank=True) #FUNCTIONS def creating_session(subsession: Subsession): # set the payoff round if subsession.round_number == 1: for p in subsession.get_players(): p.random_num = random.randint(1, 2) # PAGES class YourChoice1(Page): @staticmethod def is_displayed(player: Player): return player.random_num == 1 form_model = 'player' form_fields = ['choice', 'preference'] class YourChoice2(Page): @staticmethod def is_displayed(player: Player): return player.random_num == 2 form_model = 'player' form_fields = ['choice', 'preference'] class TaskCM(Page): form_model = 'player' form_fields = ['CM'] @staticmethod def is_displayed(player: Player): return player.choice == "CM" @staticmethod def before_next_page(player, timeout_happened): if player.session.payoff_round == player.participant.task_num: # determine the result for payoff calculation result = player.CM print("Last task result:", result) # determine the payoff Bayesian = 25 # change to be right # the truth is either left 0 or right 1 truth = 0 # determine the truth num0 = random.randint(1, 100) print("Last task num0:", num0) if num0 > Bayesian: truth = 1 print("Last task truth:", truth) # determine the payoff based on the truth num1 = random.randint(0, 100) num2 = random.randint(0, 100) print("Last task num1:", num1) print("Last task num2:", num2) if truth == 0: if result > num1 or result > num2: player.payoff = C.BONUS else: player.payoff = 0 if truth == 1: if result < num1 or result < num2: player.payoff = C.BONUS else: player.payoff = 0 print("Last task payoff:", player.payoff) class TaskDBM(Page): form_model = 'player' form_fields = ['DBM_left', 'DBM_right', 'DBM_list'] @staticmethod def is_displayed(player: Player): return player.choice == "DBM" @staticmethod def before_next_page(player, timeout_happened): if player.session.payoff_round == player.participant.task_num: # determine the result for payoff calculation result = random.randint(player.DBM_left, player.DBM_right) print("Last result:", result) # determine the payoff Bayesian = 8 # change to be right # the truth is either left 0 or right 1 truth = 0 # determine the truth num0 = random.randint(1, 100) print("Last task num0:", num0) if num0 > Bayesian: truth = 1 print("Last task truth:", truth) # determine the payoff based on the truth num1 = random.randint(0, 100) num2 = random.randint(0, 100) print("Last task num1:", num1) print("Last task num2:", num2) if truth == 0: if result > num1 or result > num2: player.payoff = C.BONUS else: player.payoff = 0 if truth == 1: if result < num1 or result < num2: player.payoff = C.BONUS else: player.payoff = 0 print("Last task payoff:", player.payoff) class Results(Page): @staticmethod def before_next_page(player, timeout_happened): player.participant.finished = True class Instruction3(Page): pass class Survey(Page): form_model = 'player' form_fields = ['knowledge', 'estimate', 'feedback1', 'feedback2', 'feedback3'] page_sequence = [Instruction3, YourChoice1, YourChoice2, TaskCM, TaskDBM, Survey, Results,]