from otree.api import * c = cu doc = '' class C(BaseConstants): NAME_IN_URL = 'Lottery' PLAYERS_PER_GROUP = None NUM_ROUNDS = 15 LOTTERIES = ('low_5', 'low_9.5', 'low_18', 'low_34', 'low_65', 'mid_5', 'mid_9.5', 'mid_18', 'mid_34', 'mid_65', 'high_5', 'high_9.5', 'high_18', 'high_34', 'high_65') class Subsession(BaseSubsession): pass def creating_session(subsession: Subsession): session = subsession.session import random import itertools treatments = itertools.cycle(itertools.product([1, 2])) if subsession.round_number == 1: for p in subsession.get_players(): round_numbers = list(range(1, C.NUM_ROUNDS + 1)) random.shuffle(round_numbers) task_rounds = dict(zip(C.LOTTERIES, round_numbers)) p.participant.task_rounds = task_rounds treatment = next(treatments) print('treatment is', treatment) p.participant.counterbalance = treatment[0] order = [random.choice([1, 2]) for _ in range(15)] p.participant.order1 = order[0] p.participant.order2 = order[1] p.participant.order3 = order[2] p.participant.order4 = order[3] p.participant.order5 = order[4] p.participant.order6 = order[5] p.participant.order7 = order[6] p.participant.order8 = order[7] p.participant.order9 = order[8] p.participant.order10 = order[9] p.participant.order11 = order[10] p.participant.order12 = order[11] p.participant.order13 = order[12] p.participant.order14 = order[13] p.participant.order15 = order[14] print(order) class Group(BaseGroup): pass class Player(BasePlayer): Lottery = models.IntegerField(choices=[[1, 'Lottery 1'], [2, 'Lottery 2']], label='', widget=widgets.RadioSelectHorizontal) time_spent = models.FloatField() class Low_5(Page): form_model = 'player' form_fields = ['Lottery', 'time_spent'] @staticmethod def is_displayed(player: Player): participant = player.participant return player.round_number == participant.task_rounds['low_5'] @staticmethod def before_next_page(player: Player, timeout_happened): participant = player.participant if participant.order1 == 1 & player.Lottery == 1: participant.low_5 = "safe" elif participant.order1 == 2 & player.Lottery == 2: participant.low_5 = "safe" else: participant.low_5 = "risky" class Low_9_5(Page): form_model = 'player' form_fields = ['Lottery', 'time_spent'] @staticmethod def is_displayed(player: Player): participant = player.participant return player.round_number == participant.task_rounds['low_9.5'] @staticmethod def before_next_page(player: Player, timeout_happened): participant = player.participant if participant.order2 == 1 & player.Lottery == 1: participant.low_9_5 = "safe" elif participant.order2 == 2 & player.Lottery == 2: participant.low_9_5 = "safe" else: participant.low_9_5 = "risky" class Low_18(Page): form_model = 'player' form_fields = ['Lottery', 'time_spent'] @staticmethod def is_displayed(player: Player): participant = player.participant return player.round_number == participant.task_rounds['low_18'] @staticmethod def before_next_page(player: Player, timeout_happened): participant = player.participant if participant.order3 == 1 & player.Lottery == 1: participant.low_18 = "safe" elif participant.order3 == 2 & player.Lottery == 2: participant.low_18 = "safe" else: participant.low_18 = "risky" class Low_34(Page): form_model = 'player' form_fields = ['Lottery', 'time_spent'] @staticmethod def is_displayed(player: Player): participant = player.participant return player.round_number == participant.task_rounds['low_34'] @staticmethod def before_next_page(player: Player, timeout_happened): participant = player.participant if participant.order4 == 1 & player.Lottery == 1: participant.low_34 = "safe" elif participant.order4 == 2 & player.Lottery == 2: participant.low_34 = "safe" else: participant.low_34 = "risky" class Low_65(Page): form_model = 'player' form_fields = ['Lottery', 'time_spent'] @staticmethod def is_displayed(player: Player): participant = player.participant return player.round_number == participant.task_rounds['low_65'] @staticmethod def before_next_page(player: Player, timeout_happened): participant = player.participant if participant.order5 == 1 & player.Lottery == 1: participant.low_65 = "safe" elif participant.order5 == 2 & player.Lottery == 2: participant.low_65 = "safe" else: participant.low_65 = "risky" class Mid_5(Page): form_model = 'player' form_fields = ['Lottery', 'time_spent'] @staticmethod def is_displayed(player: Player): participant = player.participant return player.round_number == participant.task_rounds['mid_5'] @staticmethod def before_next_page(player: Player, timeout_happened): participant = player.participant if participant.order6 == 1 & player.Lottery == 1: participant.mid_5 = "safe" elif participant.order6 == 2 & player.Lottery == 2: participant.mid_5 = "safe" else: participant.mid_5 = "risky" class Mid_9_5(Page): form_model = 'player' form_fields = ['Lottery', 'time_spent'] @staticmethod def is_displayed(player: Player): participant = player.participant return player.round_number == participant.task_rounds['mid_9.5'] @staticmethod def before_next_page(player: Player, timeout_happened): participant = player.participant if participant.order7 == 1 & player.Lottery == 1: participant.mid_9_5 = "safe" elif participant.order7 == 2 & player.Lottery == 2: participant.mid_9_5 = "safe" else: participant.mid_9_5 = "risky" class Mid_18(Page): form_model = 'player' form_fields = ['Lottery', 'time_spent'] @staticmethod def is_displayed(player: Player): participant = player.participant return player.round_number == participant.task_rounds['mid_18'] @staticmethod def before_next_page(player: Player, timeout_happened): participant = player.participant if participant.order8 == 1 & player.Lottery == 1: participant.mid_18 = "safe" elif participant.order8 == 2 & player.Lottery == 2: participant.mid_18 = "safe" else: participant.mid_18 = "risky" class Mid_34(Page): form_model = 'player' form_fields = ['Lottery', 'time_spent'] @staticmethod def is_displayed(player: Player): participant = player.participant return player.round_number == participant.task_rounds['mid_34'] @staticmethod def before_next_page(player: Player, timeout_happened): participant = player.participant if participant.order9 == 1 & player.Lottery == 1: participant.mid_34 = "safe" elif participant.order9 == 2 & player.Lottery == 2: participant.mid_34 = "safe" else: participant.mid_34 = "risky" class Mid_65(Page): form_model = 'player' form_fields = ['Lottery', 'time_spent'] @staticmethod def is_displayed(player: Player): participant = player.participant return player.round_number == participant.task_rounds['mid_65'] @staticmethod def before_next_page(player: Player, timeout_happened): participant = player.participant if participant.order10 == 1 & player.Lottery == 1: participant.mid_65 = "safe" elif participant.order10 == 2 & player.Lottery == 2: participant.mid_65 = "safe" else: participant.mid_65 = "risky" class High_5(Page): form_model = 'player' form_fields = ['Lottery', 'time_spent'] @staticmethod def is_displayed(player: Player): participant = player.participant return player.round_number == participant.task_rounds['high_5'] @staticmethod def before_next_page(player: Player, timeout_happened): participant = player.participant if participant.order11 == 1 & player.Lottery == 1: participant.high_5 = "safe" elif participant.order11 == 2 & player.Lottery == 2: participant.high_5 = "safe" else: participant.high_5 = "risky" class High_9_5(Page): form_model = 'player' form_fields = ['Lottery', 'time_spent'] @staticmethod def is_displayed(player: Player): participant = player.participant return player.round_number == participant.task_rounds['high_9.5'] @staticmethod def before_next_page(player: Player, timeout_happened): participant = player.participant if participant.order12 == 1 & player.Lottery == 1: participant.high_9_5 = "safe" elif participant.order12 == 2 & player.Lottery == 2: participant.high_9_5 = "safe" else: participant.high_9_5 = "risky" class High_18(Page): form_model = 'player' form_fields = ['Lottery', 'time_spent'] @staticmethod def is_displayed(player: Player): participant = player.participant return player.round_number == participant.task_rounds['high_18'] @staticmethod def before_next_page(player: Player, timeout_happened): participant = player.participant if participant.order13 == 1 & player.Lottery == 1: participant.high_18 = "safe" elif participant.order13 == 2 & player.Lottery == 2: participant.high_18 = "safe" else: participant.high_18 = "risky" class High_34(Page): form_model = 'player' form_fields = ['Lottery', 'time_spent'] @staticmethod def is_displayed(player: Player): participant = player.participant return player.round_number == participant.task_rounds['high_34'] @staticmethod def before_next_page(player: Player, timeout_happened): participant = player.participant if participant.order14 == 1 & player.Lottery == 1: participant.high_34 = "safe" elif participant.order14 == 2 & player.Lottery == 2: participant.high_34 = "safe" else: participant.high_34 = "risky" class High_65(Page): form_model = 'player' form_fields = ['Lottery', 'time_spent'] @staticmethod def is_displayed(player: Player): participant = player.participant return player.round_number == participant.task_rounds['high_65'] @staticmethod def before_next_page(player: Player, timeout_happened): participant = player.participant if participant.order15 == 1 & player.Lottery == 1: participant.high_65 = "safe" elif participant.order15 == 2 & player.Lottery == 2: participant.high_65 = "safe" else: participant.high_65 = "risky" page_sequence = [Low_5, Low_9_5, Low_18, Low_34, Low_65, Mid_5, Mid_9_5, Mid_18, Mid_34, Mid_65, High_5, High_9_5, High_18, High_34, High_65]