from otree.api import * from markupsafe import Markup from random import choice doc = """ Implementation of risk preference elicitation method from Eckel and Grossman 2002. Sex differences and statistical stereotyping in attitudes toward financial risk. Specifically from Dave et al. 2010. Eliciting risk preferences: When is simple better? (see Appendix) """ class C(BaseConstants): NAME_IN_URL = 'ri_eg' PLAYERS_PER_GROUP = None NUM_ROUNDS = 1 lotteries = dict( high=[28, 36, 44, 52, 60, 70], low=[28, 24, 20, 16, 12, 2] ) lottery_choices = [(n, f'Lotteria {n}') for n in range(1, 7)] minutes_for_lottery = 2 class Subsession(BaseSubsession): pass class Group(BaseGroup): pass class Player(BasePlayer): timeout = models.BooleanField(initial=False) lottery = models.IntegerField( label=Markup('Seleziona la lotteria qui sotto:'), choices=C.lottery_choices ) lottery_win = models.BooleanField(default=0) # FUNCTIONS def set_payoff(player: Player, const: C): participant = player.participant participant.vars['lottery_choice'] = player.lottery lottery_win = choice([False, True]) player.lottery_win = lottery_win participant.vars['lottery_win'] = lottery_win payoff = const.lotteries['high'][player.lottery - 1] if lottery_win else \ const.lotteries['low'][player.lottery - 1] player.payoff = payoff participant.lottery_payoff = payoff # PAGES class Lottery(Page): form_model = 'player' form_fields = ['lottery'] timeout_seconds = 60 * C.minutes_for_lottery @staticmethod def vars_for_template(player: Player): return dict( lotteries=[dict(high=h, low=l) for h, l in zip(*C.lotteries.values())] ) @staticmethod def before_next_page(player: Player, timeout_happened): if timeout_happened: player.timeout = True player.participant.dropout = True else: set_payoff(player, C) @staticmethod def app_after_this_page(player: Player, upcoming_apps): participant = player.participant if participant.dropout: return upcoming_apps[-1] page_sequence = [Lottery]