from otree.api import ( models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, Currency as c, currency_range, ) class Constants(BaseConstants): name_in_url = 'pg1' #何人で1グループかを決める players_per_group = 4 #同じことを何回するか ラウンド数 num_rounds = 2 instructions_template = 'pg1/instructions.html' class Subsession(BaseSubsession): pass class Group(BaseGroup): #関数を作る #関数の外で定義されている変数を関数で利用するには self設定する def cal_point(self): #プレイヤーが入力した貢献値を合計して平均し,それを利得にする。 players = self.get_players() # players = [P1,P2,P3,P4] というように,データの各行の数値が全部ひっぱってくる # 関数を呼び出すときは変数と区別がつかなくなるので()をつけておく player_num = len(players) # データセットの中の行の数 # あるいは player_num = Constants.players_per_group total_contribution = 0 for player in players: #for文はloop total_contribution += player.contribution # total_contribution = total_contribution + player.contribution # プレイヤーの行のデータのうちplayer.contributionでcontributionをとってくる。 player.shojikin -= player.contribution # 残された所持金を表示 total_contribution *= 2 avg = round(total_contribution / player_num) # 又は total_contribution // player_num で小数点以下切り捨て # ritokuをIntegerFieldで指定しているため # 実はroundは一番近い「偶数」を返す for player in players: player.point = avg player.ritoku = player.shojikin + player.point class Player(BasePlayer): #変数を作ろう #みんなが入力した数値を受取る受け皿 contribution = models.IntegerField(label='' ) point = models.IntegerField() #ritokuは入力させる表示を作らないので,labelは不要 ritoku = models.IntegerField() # endowmentの設定 shojikin = models.IntegerField(initial = 1000)