Participant variables

1,085 views
Skip to first unread message

aline....@gmail.com

unread,
Jan 30, 2022, 12:16:46 PM1/30/22
to oTree help & discussion
I have the following question regarding participant variables. How would I have to change the below code in order to store the following variables (referenced in template) as participant variables so I can use them again at the very end of the experiment?

{{ chosen_probability }}
{{ chosen_return }}
{{ chosen_endowment }}
{{ chosen_investment }}
{{ payoff }}

In init.py:

class Constants(BaseConstants):
name_in_url = 'gneezy_potters'
# group size
players_per_group = None
# endowment amount for each different round
endowment = (20, 20, 20, 20)
# probability of success
probability = (1 / 2, 1 / 2, 1 / 2, 1 / 2)
# multiplier = return rate + 1
multiplier = (1.7, 1.35, 1.5, 1.35)
# multiplier2 = return rate2 + 1
multipliersecond = (0.7, 0.75, 0.75, 0.7)
# the number of total rounds
num_rounds = len(probability)

class Results(Page):
# skip results until last page
# ----------------------------------------------------------------------------------------------------------------
@staticmethod
def is_displayed(player: Player):
# if Constants.one_choice_per_page:
return player.subsession.round_number == Constants.num_rounds
# return True

@staticmethod
def vars_for_template(player: Player):
# round_num = self.subsession.round_number
rand_num = player.subsession.rand_round
# unzip <cem_choices> into list of lists
choices = player.participant.vars['environment'][rand_num - 1]
endowment = choices[1]
probability = round(choices[2], 2)
if player.winner == 1:
return_rate = round(choices[3], 2)
else:
return_rate = round(choices[4], 2)
return {
'round_to_pay': rand_num,
'chosen_endowment': endowment,
'chosen_investment': player.participant.vars['investment'][rand_num - 1],
'chosen_probability': probability,
'chosen_return': return_rate,
'payoff': player.participant.payoff,
}

In template:
{% extends "global/Page.html" %}
{% load staticfiles otree %}

{% block title %}
Resultat
{% endblock %}

{% block content %}
<p> Die Wahrscheinlichkeit einer positiven
Entwicklung der Anlage lag bei <b>{{ chosen_probability }}</b> und die Rendite war <b>{{ chosen_return }}</b>.
Ihr Ausstattung in der Runde war <b>{{ chosen_endowment }} und Sie investierten <b>{{ chosen_investment }}</b>
in die riskante Anlage. Somit liegt die Auszahlung dieser Runde bei <b>{{ payoff }}</b>.
</p>
{% next_button %}
{% endblock %}

Chris @ oTree

unread,
Jan 30, 2022, 5:31:44 PM1/30/22
to aline....@gmail.com, oTree help & discussion
In vars_for_template, first define:

participant = player.participant

Then do:

participant.probability = probability

You must declare a participant field called probability in settings.py. (PARTICIPANT_FIELDS)

But note that a new value will be generated each time the page is refreshed. So probably better to define these values in before_next_page of the previous page. 

Sent from my phone

On Jan 31, 2022, at 1:16 AM, aline....@gmail.com <aline....@gmail.com> wrote:

I have the following question regarding participant variables. How would I have to change the below code in order to store the following variables (referenced in template) as participant variables so I can use them again at the very end of the experiment?
--
You received this message because you are subscribed to the Google Groups "oTree help & discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to otree+un...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/otree/f389f4c9-5500-4640-9dc6-a6677ac2ff87n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages