| Reference | Downloads | Github

Question about loops and randomized conditions

Windows 10
PsychoPy version 1.83.04
Hey I am an undergrad student working on my thesis. I very new to psychopy, and know very little about python (but I can learn and know a little about coding).

I am doing a cognitive condition task. Where a word appears in the centre of the screen, followed by a target stimuli located at the top or bottom of the screen depending on word valence ratings from a previous experiment. Each trial is followed by a test to see if participants actually read the word that appear by asking them to select one of two words.
What I am trying to do-
For the word reading test after each trial, I want to choose what the other word may be, but randomly choose 1 word out of the other 39 in that category. For example, if I have a list of 40 positive words and on this specific trial happy is the stimuli presented, followed by the target stimuli appearing up. I want the other words that could be chosen, also be out of that list of the remaining 39.

What did you try to make it work?
Well I tried brute forcing it first to make sure that it actually works, but the issue is that based on my other conditions (timer, word stimuli, L/R for where the target word appears in the recognizing task, and the pool of other words. My excel file begins to have way too many trials (40k+). What I want is to be able to tell psychopy to choose 1 of the 39 other words, and still have know which was the word stimuli being presented for each trial. Each target word may appear only 10 times, but the other word that they have to choose from needs to be unique each iteration.
I could also continue to bruteforce it, and have a unique iteration of the experiment for each participant, where I randomly pick which words will be paired with with conditions. I know there is an easier way, but not sure how.


This is an example where it seems like you want to separate your list of 40 positive words from appearing in your conditions file. Instead, you will manually read them in via some custom code into a list, randomise it, and them and then present one word per trial.

This way, your conditions file then just gets to be the length it needs to be, depending on the balancing of your other variables.

So create a simple text file that contains nothing other than 40 rows of positive words, called, say, positive_words.txt:


Then insert a “code component” from the “custom” component panel. This component provides a number of tabs where you can type in code that will then run at specific times in the session.

In the “begin experiment” tab:

# read your words into a list:
with open('positive_words.txt') as file: positive_words = 

# randomise it:

Then in the “begin routine” tab:

# get the next word from the list:
current_positive_word = positive_words.pop()

# need to ensure we manually store this trial's word in the data file:
thisExp.addData('positive_word', current_positive_word)

You now have a variable current_positive_word that you can use in your text stimuli and so on for this trial. Make sure the code component is above any text stimuli that might use it, so they get access to the current value.

Dear Michael,

Thanks so much for the this intro to it. If I understand what you have done, is create a list that allows the words to be chosen for that specific trial. The issue is that it chooses randomly between two lists of words (negative and positive) for the stimuli. Then for the test, needs to know which word it used previously, and then know which list it came from to be able to choose another word from the same list. With that, it also needs to random so that all words are used as stimuli the same amount of times, and the words tests are always different.
From my understanding talking briefly with my lab mates, I would need to create a list of used words and make psychopy do a check to choose a word from poslist or neglist, and make sure its not in pos_used or neg_used. Would this be the correct line of thinking?

It selects a word for every trial.

No, only a single list of positive words, which was what was stated in your original post. To get suggestions on forums like this that do what you want, you need to carefully describe your actual requirements.

Yes, it does that, along as the number of trials is equal to the length of the word list.

No, an entry is removed from the list on every trial (via the .pop() function). i.e. the list decreases in size on each trial. No repetition is possible (assuming the list contains only unique values), because the value is no longer available in the list for future selections. So no, you don’t need to keep track of what words have been used.