from otree.api import * import random doc = """ Risk task. Akdeniz & Mathew, 2022, Normative versus prosocial underpinnings of human decision making. """ class Constants(BaseConstants): players_per_group = None num_rounds = 1 name_in_url = 'part3investment' endowment = 250 multiplier = float(2.5) # multiplier for the asset, in case successful prob = float(0.5) # success probability task = 3 class Subsession(BaseSubsession): pass def creating_session(subsession: Subsession): if subsession.round_number == 1: for player in subsession.get_players(): player.part3task = random.randint(1, 3) print('part3task is', player.part3task) class Group(BaseGroup): pass class Player(BasePlayer): ex1 = models.CurrencyField( doc="""Answer to first example""", min=0, max=Constants.multiplier*Constants.endowment, label="", ) ex2 = models.CurrencyField( doc="""Answer to second example""", min=0, max=Constants.multiplier*Constants.endowment, label="", ) invested = models.IntegerField( min=0, max=Constants.endowment, label="How much do you want to invest in the asset?", doc="""Invested""" ) not_invested = models.CurrencyField(min=0, max=Constants.endowment, doc="""Not invested""") success = models.BooleanField() # investment_outcome = models.BooleanField() # investment = models.IntegerField() payment_investment = models.CurrencyField() part3task = models.IntegerField() payment_part3 = models.CurrencyField(initial=0) choice_part3 = models.IntegerField(initial=0) round = models.IntegerField(initial=0) chosen_task3 = models.StringField() # Set payoffs assigns the rewards for each round. def set_payoffs_risk(self, player): self.not_invested = Constants.endowment - cu(self.invested) # participant = player.participant # self.investment = self.invested draw_invest = random.random() print('The random draw for the success of investment in round', self.round_number, 'is', draw_invest) if draw_invest < Constants.prob: self.payment_investment = Constants.multiplier * cu(self.invested) + cu(self.not_invested) self.success = 1 else: self.payment_investment = cu(self.not_invested) self.success = 0 # self.investment_outcome = self.success # print('The payoff in round', self.round_number, 'is', self.payment_investment) # print('invested amount', self.invested) # print('investment outcome', self.investment_outcome) def set_payoffs_part3(self, player): participant = player.participant if self.part3task == 1: # print(participant.payment_roll, participant.choice_roll, participant.chosen_round) self.payment_part3 = participant.payment_roll self.choice_part3 = participant.choice_roll self.round = participant.chosen_round self.chosen_task3 = "die-rolling" elif self.part3task == 2: self.payment_part3 = participant.payment_bucket self.choice_part3 = participant.bucket_choice # self.round = 0 self.chosen_task3 = "bucket" else: self.payment_part3 = self.payment_investment self.choice_part3 = self.invested self.round = self.success self.chosen_task3 = "investment" print(self.payment_part3, self.choice_part3, self.round, self.chosen_task3) # PAGES class StartInv(Page): pass class Instructions(Page): form_model = 'player' form_fields = ['ex1', 'ex2'] def error_message(self, value): if value["ex1"] != 430: return 'The first question is not answered correctly. Consult the instructions and try again.' elif value["ex2"] != 130: return 'The second question is not answered correctly. Consult the instructions and try again.' class StartInv2(Page): pass class Inv(Page): form_model = 'player' form_fields = ['invested'] def before_next_page(player: Player, timeout_happened): player.set_payoffs_risk(player) player.set_payoffs_part3(player) class InvResults(Page): def vars_for_template(self): return dict( success=self.success, ) page_sequence = [StartInv, Instructions, StartInv2, Inv, InvResults]