oTree Forum >

Trouble with treatments

#1 by ecopsy2023

Hello there,
I have been working to assign a treatment variable to each participant, which remains constant across each round. I believe the code below should work. However, I am having issues verifying that if works? How do I access participant fields? Do I have to add a treatment variable to the Player Class? I have followed the Documentation to this point however it seems unclear how I should proceed.

class Subsession(BaseSubsession):
    def creating_session(subsession):
        # Import itertools
        import itertools

        # Define the treatments as a list of strings
        treatments = itertools.cycle(
            ['A', 'B', 'C']
        )

        # Loop through all players and assign them a treatment
        for player in subsession.get_players():
            participant = player.participant
            participant.treatment = next(treatments)

#2 by Fanist (edited )

Hi @ecopsy2023, I think you should not include the function under class Subsession(BaseSubsession).

Instead do that:
```
class Subsession(BaseSubsession):
    pass
    
# Functions
def creating_session(subsession):
        # Import itertools
        import itertools

        # Define the treatments as a list of strings
        treatments = itertools.cycle(
            ['A', 'B', 'C']
        )

        # Loop through all players and assign them a treatment
        for player in subsession.get_players():
            participant = player.participant
            participant.treatment = next(treatments)
 ```
 And do not forget to add 'treatment' in settings.py:
 ```
 PARTICIPANT_FIELDS = ['treatment']
 ```
 So now the code helps us to assign A, B, and C one by one to each coming participant. But the assigned treatment will not change in multi-rounds. A demo I write can be seen at https://github.com/Fanisting/otree-assign-treatments

#3 by ecopsy2023

Hey @Fanist, 
Thank you again for the continued guidance. I did as you said and moved the code to the #Functions section. Furthermore I added the 'treatment' in the Participant Fields. I also followed your github example for placing it in vars_for_template. However, for some reason when I run the code, I get the following error: 

"
File "C:\Users\david\AppData\Local\Programs\Python\Python310\lib\site-packages\otree\database.py", line 260, in __get__
    return obj.vars[self.attr]
KeyError: 'treatment'
"

Any ideas why this might be happening?

#4 by Fanist

Hey @ecopsy2023, how did you show the variable 'treatment' in MyPage html?

On my webpage, the code is:

```
The assigned treatment is {{treatment}}.
```

#5 by ecopsy2023 (edited )

Hey @Fanist, 
I have done the same in my html file. I am unsure where the fault is coming from.

#6 by ecopsy2023

Upon the advice given: https://groups.google.com/g/otree/c/UfLqiPtMWlo

Resetting the database solved the issue.

#7 by Fanist

@ecopsy2023 That is a weird issue, but good to see you figure it out!

Write a reply

Set forum username