Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Supported by

Need help for experiment for display randomly narratives across participant

I need to construct an experiment with 12 short narratives, knowing that each narrative has 4 different ending, which means 48 stories. But each participant must read only 12 stories with one type of ending.
I would like to display randomly these 12 narratives with only one of their associate ending across participant and i don’t know how.


  • Dear oriane_g26,

    If I understand you correctly, I would suggest to create 12 different trials and assign each trial a factor "ending", with 4 levels ( You can then create a corresponding between subject factor (, and use get_permuted_bws_factor_condition() ( to get the corresponding ending for each participant (assuming you want the same ending for all 12 narratives per participant). This balances the endings over participants.

    To then present the 12 trials randomly, just shuffle the block you put them in (

    Something like this maybe (not tested!):

    import expyriment as xpy
    narratives = [{"beginning":"narrative1", "possible_endings": ["ending1", "ending2", "ending3", "ending4"]},
                  {"beginning":"narrative2", "possible_endings": ["ending1", "ending2", "ending3", "ending4"]},
                  {"beginning":"narrative3", "possible_endings": ["ending1", "ending2", "ending3", "ending4"]},
                  {"beginning":"narrative4", "possible_endings": ["ending1", "ending2", "ending3", "ending4"]},
                  {"beginning":"narrative5", "possible_endings": ["ending1", "ending2", "ending3", "ending4"]},
                  {"beginning":"narrative6", "possible_endings": ["ending1", "ending2", "ending3", "ending4"]},
                  {"beginning":"narrative7", "possible_endings": ["ending1", "ending2", "ending3", "ending4"]},
                  {"beginning":"narrative8", "possible_endings": ["ending1", "ending2", "ending3", "ending4"]},
                  {"beginning":"narrative9", "possible_endings": ["ending1", "ending2", "ending3", "ending4"]},
                  {"beginning":"narrative10", "possible_endings": ["ending1", "ending2", "ending3", "ending4"]},
                  {"beginning":"narrative11", "possible_endings": ["ending1", "ending2", "ending3", "ending4"]},
                  {"beginning":"narrative12", "possible_endings": ["ending1", "ending2", "ending3", "ending4"]}]  # Replace with actual narratives and endings
    exp = xpy.control.initialize()
    block1 =
    for counter, narrative in enumerate(narratives):
        trial =
        trial.set_factor("Narrative", counter+1)
        s = xpy.stimuli.TextBox(narrative["beginning"])
        trial.possible_endings = narrative["possible_endings"]
    exp.add_bws_factor("Ending", [1, 2, 3, 4])
    exp.add_data_variable_names(["Narrative", "Ending"])
    xpy.control.start()  # This will create a subject ID
    for trial in exp.blocks[0].trials:
        ending = exp.get_permuted_bws_factor_condition("Ending")
        trial.set_factor("Ending", ending)
        s = xpy.stimuli.TextBox(possible_endings[ending-1])
        for stimulus in trial.stimuli:
   [trial.get_factor("Narrative"), trial.get_factor("Ending")])

    I hope this helps, let me know if you have any further questions.

  • this helps !Thank you very much !
    i have one more question, for narratives can i import stories from a folder or do i write them directly on the script ?

  • maybe this could be efficient...

  • I just put them into my example directly in order to demonstrate the principle, but you can of course also read them from a text file.

  • Ok, thanks !
    for the selection of the type of ending i need something like random.choice() because the number of each ending correspond to a specific condition (ending1 = condition1, ending 2= condition..) but across stories participant mustn't read only one type of condition.
    I've try to change this by replace s = xpy.stimuli.TextBox(possible_endings[ending-1]) by s = xpy.stimuli.TextBox(random.choice(possible_endings)) but in data i don't have the correct index associate with the type of ending presented

Sign In or Register to comment.