from otree.api import ( models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, Currency as c, currency_range ) import random class Constants(BaseConstants): name_in_url = 'gamble_practice_v1' players_per_group = None num_rounds = 1 initial_amount = 6 class Subsession(BaseSubsession): def creating_session(self): if self.round_number == 1: paying_round_pract = random.randint(1, 3) self.session.vars['paying_round_pract'] = paying_round_pract class Group(BaseGroup): pass class Player(BasePlayer): option_choice_1 = models.BooleanField(initial = False) option_choice_2 = models.BooleanField() C = models.FloatField() w0 = models.FloatField() w1 = models.FloatField() min_g1 = models.FloatField() max_g1 = models.FloatField() C2 = models.FloatField() w2 = models.FloatField() min_g2 = models.FloatField() max_g2 = models.FloatField() final = models.FloatField() id_number = models.IntegerField(label = "Your ID", min=1, max=20) def stage_1(self): ### define first stage if self.round_number == 1: self.w0 = Constants.initial_amount self.C = round(random.uniform(0.9*self.w0, 0.99*self.w0), 1) self.min_g1 = round(random.uniform(1*self.C, 1.05*self.C), 1) self.max_g1 = round(random.uniform(1.06*self.C, 1.2*self.C), 1) self.w1 = round(random.uniform(self.min_g1, self.max_g1), 1) if self.round_number == 2: self.w0 = Constants.initial_amount self.C = round(random.uniform(1*self.w0, 1.2*self.w0), 1) self.min_g1 = round(random.uniform(0.8*self.C, 0.9*self.C), 1) self.max_g1 = round(random.uniform(0.92*self.C, 1.02*self.C), 1) self.w1 = round(random.uniform(self.min_g1, self.max_g1), 1) if self.round_number == 3: self.w0 = Constants.initial_amount self.C = round(random.uniform(0.9*self.w0, 1.1*self.w0), 1) self.min_g1 = round(random.uniform(0.95*self.C, 0.99*self.C), 1) self.max_g1 = round(random.uniform(1.01*self.C, 1.2*self.C), 1) self.w1 = round(random.uniform(self.min_g1, self.max_g1), 1) def stage_2(self): ### define second stage if self.round_number == 1: self.min_g2 = round(random.uniform(0.95*self.w1, 0.99*self.w1), 1) self.max_g2 = round(random.uniform(1.01*self.w1, 1.2*self.w1), 1) self.w2 = round(random.uniform(self.min_g2, self.max_g2), 1) if self.round_number == 2: self.min_g2 = round(random.uniform(0.98*self.w1, 1.05*self.w1), 1) self.max_g2 = round(random.uniform(1.05*self.w1, 1.2*self.w1), 1) self.w2 = round(random.uniform(self.min_g2, self.max_g2), 1) if self.round_number == 3: self.min_g2 = round(random.uniform(0.8*self.w1, 0.9*self.w1), 1) self.max_g2 = round(random.uniform(0.95*self.w1, 1.05*self.w1), 1) self.w2 = round(random.uniform(self.min_g2, self.max_g2), 1) def final_result(self): if (self.option_choice_1): if (self.option_choice_2): self.final = self.w2 else: self.final = self.w1 else: self.final = self.C