【问题标题】:in_round() function in oTreeoTree 中的 in_round() 函数
【发布时间】:2017-05-18 21:42:28
【问题描述】:

考虑一个有 3 个回合的游戏。在每一轮中,玩家都会做出选择(存储在变量 choice 中)。

现在,在第 3 轮中,我想致电 someFunction,从而访问在第 2 轮中做出的选择。

不幸的是,someFunction 返回None。我不懂为什么。如果我将函数调用放在模板文件中,一切正常。

希望得到帮助 - 我已经找了好几个小时了。

class Subsession(BaseSubsession):
    def before_session_starts(self):
        if self.round_number == 3:
            for player in self.get_players():
                player.participant.vars['someKey'] = player.someFunction()

class Player(BasePlayer):
    choice = models.CharField(initial=None,
                                choices=['A','B','C'],
                                widget=widgets.RadioSelect()) 

    def someFunction(self):
        return self.in_round(2).choice

为什么会这样?

【问题讨论】:

  • 实际错误是什么?
  • player.participant.vars['someKey'] 是 None 而不是 A、B、C(前一轮的选择变量)。
  • 但是没有解释这个变量是在哪里定义的以及它是如何被填充的。我们看到的只是Player.choice
  • 希望现在更好

标签: django otree


【解决方案1】:

before_session_starts 函数在会话开始之前执行(因此得名)。因此,当它被执行时,玩家还没有完成他/她的选择。这就是 someFunction 返回 None 的原因。

您可以在第二轮结束时设置player.participant.vars['someKey'] = self.player.choice,这将为您提供所需的结果。

class Choice(Page):
    def before_next_page(self):
        if self.player.round_number == 2:
            player.participant.vars['someKey'] = self.player.choice

【讨论】:

    猜你喜欢
    • 2023-02-14
    • 2016-12-31
    • 2021-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-05
    相关资源
    最近更新 更多