#1 by manu_oble
Hello everyone, This is my first time using this software, and I am having trouble implementing a situation. I want to be able to randomly assign a type of form to participants. The form differs according to the treatment and the order of questions as follows: class C(BaseConstants): NAME_IN_URL = 'risk_task' PLAYERS_PER_GROUP = None NUM_ROUNDS = 1 treatments = ['T1', 'T2', 'T3', 'T4'] treatment_parameters = { 'T1': {'average': 80, 'ratio': 3, 'lower': 40, 'upper': 120}, 'T2': {'average': 80, 'ratio': 9, 'lower': 16, 'upper': 144}, 'T3': {'average': '2X', 'ratio': 3, 'lower': 'X', 'upper': '3X'}, 'T4': {'average': 160, 'ratio': 9, 'lower': 32, 'upper': 288}, } class Subsession(BaseSubsession): def creating_session(self): import random for player in self.get_players(): # Assign a random treatment player.treatment = random.choice(C.treatments) # Randomly assign the order of tasks player.task_order = random.choice(['risk_first', 'social_first']) This code does not seem to work. Can you help me, please? Thank you.
#2 by BvO
If I am not mistaken, if you are using the latest version of oTree, you should place def creating_session outside of the class Subsession.
#3 by ccrabbe
Hi manu_oble - What do you mean by "does not seem to work?" What are you doing to run the code, and what result are you getting? The more detail you can provide, the easier it is for us to help. Thanks, --Chris
#4 by manu_oble (edited )
Hi Chris, I have a form that needs to be assigned to multiple participants. The participants are grouped into four according to treatments T1, T2, T3, and T4. The instructions in the form contain constants that refer to the treatments. For example, for a participant in Treatment 1, the text of their form should adapt to the parameters of the respective treatment as follows: "You earn {CONSTANT.RATIO} times more than the others... If the poor earn {CONSTANT.LOWER} and the rich {CONSTANT.UPPER}". The form should change randomly each time the participant clicks on the link. class C(BaseConstants): NAME_IN_URL = 'social_task' PLAYERS_PER_GROUP = None NUM_ROUNDS = 1 treatments = ['T1', 'T2', 'T3', 'T4'] treatment_parameters = { 'T1': {'average': 80, 'ratio': 3, 'lower': 40, 'upper': 120}, 'T2': {'average': 80, 'ratio': 9, 'lower': 16, 'upper': 144}, 'T3': {'average': '2X', 'ratio': 3, 'lower': 'X', 'upper': '3X'}, 'T4': {'average': 160, 'ratio': 9, 'lower': 32, 'upper': 288}, } class Subsession(BaseSubsession): def creating_session(self): import random for player in self.get_players(): # Assign a random treatment player.treatment = random.choice(C.treatments) # Assign treatment parameters player.treatment_params = C.treatment_parameters[player.treatment] # Randomly assign the order of tasks player.task_order = random.choice(['risk_first', 'social_first']) class Group(BaseGroup): pass class Player(BasePlayer): treatment = models.StringField() task_order = models.StringField() treatment_params = models.StringField() ... ... # PAGES class Introduction(Page): form_model = 'player' form_fields = ['consent'] class STScenario1(Page): form_model = 'player' form_fields = ['S1_Q_1', 'S1_Q_2', 'S1_Q_3', 'S1_Q_4', 'S1_Q_5', 'S1_Q_6', 'S1_Q_7', 'S1_Q_8', 'S1_Q_9', 'S1_Q_10', 'S1_Q_11'] def is_displayed(player: Player): # Affiche uniquement si le consentement est donné return player.consent == True class STScenario2(Page): form_model = 'player' form_fields = ['S2_Q_1','S2_Q_2','S2_Q_3','S2_Q_4','S2_Q_5','S2_Q_6','S2_Q_6','S2_Q_7','S2_Q_8','S2_Q_9','S2_Q_10','S2_Q_11','S2_Q_12'] def is_displayed(player: Player): # Affiche uniquement si le consentement est donné return player.consent == True page_sequence = [Introduction, STScenario1, STScenario2,]
#5 by ccrabbe
Hi manu_oble - Thank you for the description of what you need. What is happening when you run things that makes you say it "does not seem to work?" Are you getting error messages? Is the wrong thing showing up in your browser? Are you not getting the data you expect in your subjects' model fields? The more detail you can provide, the more likely it is we can help. Thanks, --Chris
#6 by manu_oble
Hi Chris When I run it, the error occurs at the HTML pages where there are texts containing the constants. Here is the error: Application error (500) UndefinedVariable: Cannot resolve the variable 'ratio' (line 8, in "ratio"). I thought about creating a variable "Traitement" which would take the values T1, T2, T3, and T4. Then, in the HTML files, create conditional texts. That is, when Traitement = T1, the constants in the text take the values of T1's parameters. But I don't know how to formalize this.