| Reference | Downloads | Github

Randomly assigning participants

OS: MacOS 10.15.3
PsychoPy version : 2020.1.2
Standard Standalone? (y/n): y

Hello, I am trying to run an online experiment involving masked priming lexical decision task that uses a Latin Square design. Currently, I have two different word lists in two different excel files (OrderA.xlsx, OrderB.xlsx) and would like to assign participants to one of the two word lists randomly.

So far, what I’ve done is added a code component in the beginning of the flow with the following code under “Begin Experiment”:

import random
condition = random.choice(('A','B'))

Then, the code component is followed by an instruction routine and two different loops for the two different word list.

When I tried to run it, it seems like it is still running both word lists for each participant. Is there a way to fix this?


This code is based from this topic.

Ever since I posted this question, I’ve also tried to replace the ‘A’ and ‘B’ with the file name (i.e., ‘OrderA.xlsx’, ‘OrderB.xlsx’), as well as using coin flip under code component. None of which worked.

To summarise, what I’m trying to do is randomly assign a list (OrderA.xlsx or OrderB.xlsx), but not both, to online users participating in the experiment. This is my first time using PsychoPy, so any help would be appreciated! :slight_smile:


  • If you want to run the experiment online, then you need to provide a JavaScript alternative to your custom Python code. The usual way to do this is to get the experiment running correctly locally (using Python in your code components), and then translate the Python to a JavaScript equivalent, which can be run online. In your code component, select “Both” or “Auto -> JS” to show another tab, to put the javaScript into.
  • Your flow panel should have only one loop. In that loop, put the variable name $condition in the condition file field. Instead of randomly choosing 'A' or 'B', select between 'OrderA.xlsx' or 'OrderB.xlsx'.
1 Like

Hi Michael,

That worked!! Thank you so much.

Just another question, is there a way to include which file was used in the data filename (e.g., userID_expname_date_A.csv or userID_expname_date_B.csv)?


I think the data file name is created very early in the experiment script, before your custom code runs, so it won’t have access to your condition variable.

But generally the better way to do this is to embed the variable within your data file, which makes it much easier to use at the analysis stage. In Python, this is done by:

thisExp.addData('condition_file', condition)
1 Like

I see! Thank you so much!!

Hi @Michael, so if I have a X number of lists that I want to randomly assign to participants and I use this code, is there a way to ensure that the same each list is used the same number of times? For instance, I have 8 lists and will recruit 80 participants. Can I ensure each list is used 10 times?

Unfortunately there isn’t a way (that I know of) for Pavlovia to know what previous participants have done. Personally I will be using Qualtrics to assign participants to conditions. Qualtrics can allocate evenly, but this will be based on the number of participants who start the experiment, rather than end it.

For more details see my crib sheet.

1 Like