oTree Forum >

Checking in which iteration of the app you are

#1 by lalalavi (edited )

I have a multi-app project in which the app sequence looks like this:

app_sequence=['instructions', 'emotion', 'simple_experiment', 'emotion', 'simple_experiment', 'emotion', 'questionnaire', 'end'],

The emotion apps are perfect copies of each other, so that's fine, but the first time we run simple_experiment it needs to be slightly different from the second. The change should look like this: 


class SplitScreen(Page):
    @staticmethod
    def vars_for_template(player):   
     
        if FIRST TIME SIMPLE_EXPERIMENT
            player.sReward = 'HR'
        else:
            player.sReward = 'LR'

        url_list = [f'memes/feed{meme}.jpeg' for meme in range(1,410)] 
        random.shuffle(url_list) 
        
        (...)

Is there any elegant way to do this? For sure one could just create a new app with this minimal change but that feels like terrible coding practice because it duplicates everything, and I am not sure how participant fields would solve this issue.

Thank you in advance!! :)

-----EDIT-----

Ok now i see this might be a bigger problem than I thought of since I can't apparently repeat apps.

#2 by BonnEconLab

Regarding the repetition of apps in app_sequence, have a look at this post from the earlier oTree Google Group:

https://groups.google.com/g/otree/c/2Z3d_SStb5o/m/kEjHjBL_BAAJ

Regarding the detection whether something has already be done/run, a standard participant variable should suffice, shouldn’t it?

That is, for instance, in settings.py include

PARTICIPANT_FIELDS = ['simple_experiment_already_run']

In one of your __init__.py files — say, that of your 'instructions' app — include

def creating_session(subsession):

    if subsession.round_number == 1:
        for player in subsession.get_players():  
            player.participant.simple_experiment_already_run = False

(See https://otree.readthedocs.io/en/latest/treatments.html#treatment-groups-multiple-rounds.)

Then, in line with your own suggestion, include something like

class SplitScreen(Page):

    @staticmethod
    def vars_for_template(player):        
        if player.participant.simple_experiment_already_run == False
            player.sReward = 'HR'
            player.participant.simple_experiment_already_run = True
        else:
            player.sReward = 'LR'        
        (...)

Write a reply

Set forum username