from otree.api import ( models, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, ) import random class Constants(BaseConstants): name_in_url = 'my_survey' players_per_group = None num_rounds = 10 instructions_template = 'my_survey/instructions.html' income = 5000 purchase_price_of_point = 500 true_point = [0, 0, 0, 0, 0, 0, 500, 700, 900, 1000] the_latter = 6 class Subsession(BaseSubsession): def creating_session( self ): for player in self.get_players(): player.purchase_point = [ 200, 500, 300, 400, 600, 200, 500, 300, 400, 600] [self.round_number - 1] player.true_point = random.choice(Constants.true_point) player.pressure = player.id_in_subsession % 2 class Group(BaseGroup): pass class Player(BasePlayer): income = models.FloatField() point = models.FloatField() investment_gain = models.FloatField() total_property = models.FloatField() purchase_point = models.FloatField() true_point = models.FloatField() total_investment = models.FloatField() price_ave = models.FloatField() pressure = models.BooleanField() true_points = models.StringField() purchase_price_of_point2 = models.FloatField() lottery_num = models.PositiveIntegerField( label ='' 'くじをいくつ購入しますか?購入しない場合は0を記入してください' '', min = 0) def lottery_num_max(self): import math return math.floor( Constants.income/self.purchase_point ) def invest_return(self, lottery_num): if self.lottery_num == -1: self.total_investment = 0 * self.purchase_point self.investment_gain = 0 * self.true_point self.total_property = self.investment_gain + Constants.income - 3000 self.true_points = "なし" else: # 一旦獲得ポイントの配列を作る point_actual = [] for _ in range(lottery_num): # ランダムな値をConstants.true_pointから取り出して配列に入れる point_actual.append(random.choice(Constants.true_point)) # 表示できるようにself.true_pointsに文字列にして入れる self.true_points = str(point_actual) # 利得の計算 self.total_investment = self.lottery_num * self.purchase_point # 使った金額 self.investment_gain = sum(point_actual) # 儲けたお金 self.total_property = Constants.income + self.investment_gain - self.total_investment