Assigning participants to random sets

If this template helps then use it. If not then just delete and start from scratch.

OS (e.g. Win10): Windows 10
PsychoPy version (e.g. 1.84.x): 3
Standard Standalone? (y/n) If not then what?:Y
**What are you trying to achieve?:**Assigning participants randomly to one of three groups

What did you try to make it work?:
I’m trying to assign participants randomly to 1 of 3 groups to read a word list. I have 3 sets of word files (OrderA, OrderB, and OrderC) in three excel files.

Under assigncond routine I have added the following code in ‘begin experiment’:
import random
condition = random.choice((‘OrderA.xlsx’or’OrderB.xlsx’or’OrderC.xlsx’))

What specifically went wrong when you tried that?:
Include pasted full error message if possible. “That didn’t work” is not enough information.

When I loop each condition individually, all three lists are displayed. I read from another post that a single loop can be used- although I don’t know what should be included under the conditions field in the loop. I am new to PsychoPy. Would really appreciate any help on this.


Dear rita

I attach three dummy condition-files and a toy-psyexp for illustration.

conditiongroup1.xlsx (8.4 KB) conditiongroup2.xlsx (8.4 KB) conditiongroup3.xlsx (8.4 KB) Demo.psyexp (7.2 KB)

In the first routine of your experiment before you access any condition-file insert a code-component.

conditionFile = " "

if int(expInfo['participant'])%3 == 0:
    conditionFile = "conditiongroup3.xlsx"
elif int(expInfo['participant'])%2 == 0:
    conditionFile = "conditiongroup2.xlsx"
    conditionFile = "conditiongroup1.xlsx"

Then in the loop refer to the particular condition file by $conditionFile in the Conditions field of the loop. Do not forget to add the three Excelfiles to Additional Resources (Experiment Settings - Online) if you want to run the experiment online. I used the participant id to assign the different files but you could use any other information that you define in the Experiment info.

Best wishes Jens

1 Like

Dear Jens,
Thank you so much!
If I want to do a second trial, again assigning participants randomly to any three different conditions, I will just need to change the names of the file in the code in another start routine?
And upload the files in settings?


Dear Rita,

what do you mean by second trial? At the moment, I would describe the experiment as having three trials while each participant sees from one of three conditions (there are three 1s in conditiongroup1, three 2s in conditiongroup2 aso.) based on the participant number.

It is best to describe the experiment as you would describe it in the Method section of your manuscript. In this way others can follow :wink:

Best wishes Jens

Hi Jens,
Thanks again for your reply. This is what I want to design:
welcome → practice memory list → practice recognition → test cond1/2/3 memory list → recognition1 → test cond 4/5/6 → recognition2.

Just like the demo you did for me for the first stage random assignment (cond1/2/3), I want to do a second block assigning the same participant again to one of three different conditions 4/5/6 with different word lists.
1.Would it be a problem to do a practice run before the random assignment (cond1/2/3)?
2. How do I assign the participant again in the second round for cond 4/5/6? Is it the same as cond1/2/3 with changes in the code and uploading the files in settings?

Hello Rita,

ad 1) no not at all, either you use a different list aka Excelfile for practice or use the selected row property of the loop to select specific rows from your Excelfile as practice trials and other as the experimental trials. I would probably go for the first solution.

ad 2) you could use the same code for condition1/2/3 to assign participants to condition 4/5/6. I suggest to start with a toy-example, not your real material. Simply create three more Excelfiles containing 4/5/6, assign them to participants with some code as you did before :wink: and add the files as additional resources.

Do you want to combine everything with everything? Or do you want to present only specific combinations?

Best wishes Jens

Hello Jens,

Both the practice and second test worked, just like the first one I designed :wink:
I could not do it using another 3 Excel files (4/5/6). It seems to pick up the same conditiongroup for test 1 and again in test 2 (e.g conditiongroup4 in test 1 and again in Test 2), although I added all files to Resources. So I combined the list and selected rows. I am not looking for specific combinations- so it works fine.
Thanks again! :grinning:
Best wishes,

Hello Rita

do mind showing how you tried to achieve this? Keep in mind that you need another variable to store the condition-file.

Best wishes Jens

Hello Jens

What do you mean by another variable to store the condition-file?

I did the same thing for test 1 and 2 by using the same code (just changing conditiongroup name to 4, 5,6), and uploading the extra files in additional resources.
Now I have combined wordlist from (e.g conditiongroup1 and 4) and selected rows for test 1 and test 2 -Excel sheet attached.

I’m also trying to provide feedback for the practice recall and have some problem here.
I have stored correct answer and my excel file is like the one attached but it does not work.

response.xlsx (8.5 KB) two methods.xlsx (9.7 KB)

Best wishes,

Hello Rita

you need to store the value of the condition-file for the second block in a variable (e.g. conditionBlock2) different from the variable (e.e.g conditionBlock1) in the first block. conditionBlockN go into the respective loop. But given that selected rows worked for you and you don’t need complete randomization of all condition-files, I guess you are fine.

Best wishes Jens

Hello Jens,
That makes sense! Thanks for explaining. Its good to know a few different ways of doing this.

Do you have any idea on the response feedback I mentioned in the post above?

Best wishes,

Dear Rita

here some code I use to provide feedback:

if resp.corr:
    msg = "richtig RT = %.3f" %(resp.rt)
    msgColor = "green"
    msg = "falsch RT = %.3f" %(resp.rt)
    msgColor = "red"

Note that this does not autotranslate to proper PsychoJS. You have to code this manually and set the code-component to both:

if (resp.corr) {
    msg = "richtig RT =" + resp.rt.toFixed(3) + " sec.";
    msgColor = "green";
} else {
    msg = "falsch RT =" + resp.rt.toFixed(3) + " sec.";
    msgColor = "red";

resp is the name of the keyboard-component and registered in the routine preceding the feedback-routine. There is a variable named correct that refers to the column correct in an Excel-file which contains the correct key to be pressed.


msg is the variable that holds the feedback-message. The code-component determining the feedback must occur above the feedback text-component (see below, ignore the element tastenText. This is special to this experiment.).

Best wishes Jens

Hello Jens
There was a minor error in my code. Fixed now!
Thank you!

Best wishes,

Lieber Jens,

I also tried to follow your dummy-demo (thanks for that!), although it’s doesn’t work for me despite implementing all the steps. When running online, it says that my pendant to your “group” parameter is not defined, although I checked like 2516 times and everything should be consistent. Any tipps?

Thank you very much in advance!
Liebe Grüße