from otree.api import ( models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, Currency as c, currency_range, ) class Constants(BaseConstants): name_in_url = 'pg' #何人で1グループかを決める players_per_group = 3 #同じことを何回するか ラウンド数 num_rounds = 1 class Subsession(BaseSubsession): pass class Group(BaseGroup): #関数を作る #関数の外で定義されている変数を関数で利用するには self設定する def cal_ritoku(self): #プレイヤーが入力した貢献値を合計して平均し,それを利得にする。 players = self.get_players() # players = [P1,P2,P3] というように,データの各行の数値が全部ひっぱってくる # 関数を呼び出すときは変数と区別がつかなくなるので()をつけておく 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をとってくる。 avg = round(total_contribution / player_num) # 又は total_contribution // player_num で小数点以下切り捨て # ritokuをIntegerFieldで指定しているため for player in players: player.ritoku = avg class Player(BasePlayer): #変数を作ろう #みんなが入力した数値を受取る受け皿 contribution = models.IntegerField(label='' ) #ritokuは入力させる表示を作らないので,labelは不要 ritoku = models.IntegerField()