Participant variables

244 views
Skip to first unread message

aline....@gmail.com

unread,
Feb 23, 2022, 7:39:12 AM2/23/22
to oTree help & discussion

Hello,

I keep receiving the following error message when storing and accessing a participant variable:

NameError: name 'Schaden' is not defined

Does anyone have a hint as to why it does not work?

In settings.py:

SESSION_CONFIGS = [
    dict(
        name="my_survey",
        display_name="EXPERIMENT",
        num_demo_participants=5,
        app_sequence=["Intro", "Test", "Aufgabe", "Preferences", "gneezy_potters", "neg_recip", "survey"],
                PARTICIPANT_FIELDS = ['payoff', 'Schaden', 'total',]
    )
]

In init.py:

class Preferences(Page):
form_model = 'player'
form_fields = ['Schaden',
'Interaktion']
@staticmethod
def is_displayed(player: Player):
return player.color == 'tmt1'

def before_next_page(player: Player, timeout_happened) :
participant = player.participant
participant.Schaden = Schaden

Chris @ oTree

unread,
Feb 23, 2022, 7:46:45 AM2/23/22
to oTree help & discussion
participant.Schaden = player.Schaden

aline....@gmail.com

unread,
Feb 23, 2022, 8:22:24 AM2/23/22
to oTree help & discussion
Thank you, but now I'm getting the following error message:

TypeError: player.Schaden is None. Accessing a null field is generally considered an error. Or, if this is intentional, use field_maybe_none()

Traceback

File /app/.heroku/python/lib/python3.9/site-packages/otree/database.py, line 589, in __getattribute__ +

File Preferences/__init__.py, line 60, in vars_for_template

57.

58.     def vars_for_template(player: Player) :

59.         participant = player.participant

60.         participant.Schaden = player.Schaden

aline....@gmail.com

unread,
Feb 24, 2022, 1:03:04 AM2/24/22
to oTree help & discussion
It is working now, but i have another question; how can I add this participant.Schaden variable to my participant.payoff variable? This is not working (see below). Does anyone have a suggestion?

class Results(Page):
form_model = 'player'
@staticmethod
def vars_for_template(player: Player):
player.total=participant.schaden + participant.payoff

Chris @ oTree

unread,
Feb 25, 2022, 2:21:23 AM2/25/22
to oTree help & discussion
The usual way is to add it to player.payoff, e.g.:

player.payoff += player.schaden

This will then be reflected in participant.payoff, which automatically contains the sum of player.payoff from all rounds.

aline....@gmail.com

unread,
Feb 25, 2022, 5:40:01 AM2/25/22
to oTree help & discussion
Thank you. And if I want it to show up on the last page both as individual variables as well as the sum, could i create a new variable that adds them together? How would the procedure look like in this case?
Reply all
Reply to author
Forward
0 new messages