简体   繁体   English

我如何只为Otree中角色的玩家求和属性值

[英]How can i sum the values of an attribute only for the players of a role in Otree

I am programming a version of the public goods game but using roles. 我正在编写公共物品游戏的一个版本,但使用角色。 Because of that, i cant obtain the sum of the total contributions of the players who are from a determined role. 因此,我无法确定角色确定的球员的总贡献。 I tried with the following code: 我尝试使用以下代码:

self.total_contribution1 = sum( [p.contribution for p in self.get_players() if p.role== 'A']) self.total_contribution1 = sum([[如果p.role =='A',p。self.get_players()中p的贡献])

But it didn't worked and i only obtained an error of Invalid Operation 但是它没有用,我只得到了无效操作的错误

If i dont write the "if part" of that code, it runs, but the values of the attributes like that one become 0 如果我不编写该代码的“如果部分”,它将运行,但是像那个那样的属性值变为0

from otree.api import (
models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer,
Currency as c, currency_range
)
import random
import floppyforms as forms
import config_leex_1

author = 'Your name here'

doc = """
Simple public goods game
"""


class Constants(BaseConstants):
name_in_url = 'tiebout_incertidumbre'
players_per_group = 4
num_rounds = config_leex_1.PEMG_number_rounds #10
instructions_template = 'beec_mg/Instructions.html'
multiplier1 = 1
multiplier2 = 2
multiplier3 = 3
i = 1
j = 1
k = 1

class Subsession(BaseSubsession):
def creating_session(self):

    for p in self.get_players():
        p.endowment = random.randint(1, 100)
        p.prob = random.randint(0,100)
        # Random creation of endowments for every round

class Group(BaseGroup):

    total_contribution1 = models.CurrencyField(initial=0)
    total_contribution2 = models.CurrencyField(initial=0)
    total_contribution3 = models.CurrencyField(initial=0)
    mean_contribution1 = models.CurrencyField(initial=0)
    mean_contribution2 = models.CurrencyField(initial=0)
    mean_contribution3 = models.CurrencyField(initial=0)
    totalp1 = models.IntegerField(initial=0)
    totalp2 = models.IntegerField(initial=0)
    totalp3 = models.IntegerField(initial=0)
    individual_share1 = models.CurrencyField(initial=0)
    individual_share2 = models.CurrencyField(initial=0)
    individual_share3 = models.CurrencyField(initial=0)

    def set_payoffs(self):
        # NOTA 1: LAS SUMAS CON FOR NO ESTÁN FUNCIONANDO, YA QUE DAN DE         
        VALOR 0
        # NOTA 2: Los sums con if botan un error de invalid operation
        # NOTA 3: Segun SO, se deberia incluir parentesis después de p.role
    # if p.role== 'A':
    # self.total_contribution1 = sum(
        # [p.contribution for p in self.get_players() if p.role() == 'A'])
        for p in self.get_players():
            if p.role() == 'A':
                self.total_contribution1=+ p.contribution

        for p in self.get_players():
            if p.role() == 'B':
                self.total_contribution2=+ p.contribution

        for p in self.get_players():
            if p.role() == 'C':
                self.total_contribution3=+ p.contribution

        # if p.role== 'B'
        # self.total_contribution2 = sum(
        # [p.contribution for p in self.get_players()])
        # if p.role== 'C'
        # self.total_contribution3 = sum(
        # [p.contribution for p in self.get_players()])

        # if p.role== 'A'
        # self.totalp1 = sum(
        # [p.counter for p in self.get_players() if p.role() == 'A'])
        for p in self.get_players():
            if p.role() == 'A':
                self.totalp1=+1

        for p in self.get_players():
            if p.role() == 'B':
                self.totalp2=+1

        for p in self.get_players():
            if p.role() == 'C':
                self.totalp3=+1

        # if p.role == 'B'
    # self.totalp2 = sum(
        # [p.counter for p in self.get_players() if p.role() == 'B'])
    # if p.role == 'C'
    # self.totalp3 = sum(
        # [p.counter for p in self.get_players() if p.role() == 'C'])

    self.individual_share1 = self.total_contribution1 *         
    Constants.multiplier1 / self.totalp1
    self.individual_share2 = self.total_contribution2 *                 
    Constants.multiplier2 / self.totalp2
    self.individual_share3 = self.total_contribution3 * 
    Constants.multiplier3 / self.totalp3

    self.mean_contribution1 = self.total_contribution1/self.totalp1
    self.mean_contribution2 = self.total_contribution2 /self.totalp2
    self.mean_contribution3 = self.total_contribution3 /self.totalp3


    for p in self.get_players():
        if p.role == "A":
            p.payoff = p.endowment - p.contribution + self.individual_share1
        elif p.role == "B":
            if p.prob <= 75:
                p.payoff = p.endowment - p.contribution + 
    self.individual_share2
            else:
                p.payoff = p.endowment - p.contribution
        else:
            if p.prob <= 50:
                p.payoff = p.endowment - p.contribution + 
    self.individual_share3
            else:
                p.payoff = p.endowment - p.contribution

class Player(BasePlayer):
    prob =models.IntegerField(min=0, max=100)
    endowment = models.CurrencyField(min=0, max=100, initial=0)
    contribution = models.CurrencyField(verbose_name='Deslice hasta 
    seleccionar la cantidad a enviar deseada',
                                    min=0,max=100,
                                    initial=0,widget=widgets.SliderInput())
    counter = models.IntegerField(initial=1)
    roller = models.IntegerField(verbose_name='Deslice hasta seleccionar el 
    número de la opción deseada',
                             min=1, max=3,
                             widget=widgets.SliderInput())
   # Se está usando un slider para esta opción pues permite evitar 
   confusiones en caso se use un grupo de botones
   # los cuales se podrían clickear en cualquier momento sin que el jugador 
   entienda si ya se registró o no su decisión

   def role(self):
      if self.roller == 1:
          return 'A'
      elif self.roller == 2:
          return 'B'
      else:
          return 'C'from otree.api import (

role is a method not a field. role是方法而不是领域。 so to make it work you need to use this: 因此,要使其正常工作,您需要使用以下命令:

self.total_contribution1 = sum( [p.contribution for p in self.get_players() if p.role() == 'A'])

Attention to the parentheses I added after role 注意我在role后添加的括号

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何使用 convolve2d 来计算两个玩家在 connect 4 中达到获胜条件的次数? - How can I use convolve2d to sum the number of times both players achieve the win condition in connect 4? 如何仅在列表列表中找到某些值的总和,并且仅当其中存在某个字符串时? - How can I find the sum of only certain values in a list of lists AND only when a certain string is in there? 如何在python中创建播放器列表 - How can I create a list of players in python Selenium中玩家的position怎么抓取? - How can I scrape the position of the players in Selenium? 如何求和数字值的结果? - How can I sum the result of number values? Python Ursina,我怎样才能获得玩家 position? - Python Ursina, How can I get the players position? 如何将变量更改为玩家偏好? - How can I change my variable to the players preference? 我如何还可以检查成员是否具有特定角色是 if 语句? 仅当您具有特定角色时才需要 elif 执行? - how can I also check if a member has a specific role is an if statement? need the elif to only execute if you have a specific role? 如何在python中将sum用于列表中的特定值? - How can I use sum in python for specific values in a list? 如何使字典返回键基于它们的值之和? - How can I make a dictionary return keys based on the sum of their values?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM