oTree Forum >

Monitor distinct variables of previous rounds

#1 by DominikBeck

Dear all,

I have a game with five rounds. After each round, I would like to moitor some variables (payoff, strategy, ...) of all previous rounds.

Currently, I have a quite nested solution: 

    def vars_for_template(player: Player):
        group = player.group

        if group.round_number == 1:
            return dict(
                ...
                )
                
        if group.round_number == 2:
            return dict(
                ...
                )
                
E.g. at round number 2, all variables from round number 1 are send to the template, all variables from further rounds are also created, but declarated with a "?" (because I have one template for all rounds)

Any tips for a lean solution? 

Best 

Dominik

#2 by JanVavra (edited )

If I understand your problem correctly I would use a for loop lopping using player objects of one participant in all rounds as follows:

def vars_for_template(player: Player):
    values_to_display = {}
    for p in player.in_all_rounds():
        value = p.field_maybe_none('field_name')
        key = f'value_in_round_{player.round_number}'
        # if value is defined in the round, use its value
        if value:
            values_to_display[key] = value
        # if not defined, replace with ?
        else:
            values_to_display[key] = "?"
            
    return values_to_display            

You could also collect the values as a list a display them on the template using loops, instead of hardcoding the values as I did here.

#3 by DominikBeck

Hello Jan, thank you really much for your response! You understood my problem and the approach helps me for the solution. 

The idea to make a dictionary and safe it in there is pretty smart! 

Instead of "f'value_in_round_{player.round_number}", I just used "p.round_number" and it also worked. 

Best, 

Dominik

Write a reply

Set forum username