Search code examples
pythonlistpsychopy

Randomising order of TextStim in an amended list/ finding the index of an iteration in a loop (Psychopy )


To get to the point, I have amended 120 different colour TextStim (different coloured word) items to a list called 'trials'. I will be looping through 'trials' to present these stimuli one by one. However, they have been amended to this list in an order according to the loop I used. I ideally need them to be randomised in order for when they are presented. I have tried:

import random

trials = random.shuffle(trials)

but all I get is TypeError: 'NoneType' object is not iterable... I think it has something to do with the fact that the type of stimulus in the list is stored as the wrong variable type. for the same reason when I try to find the trial number of each presentation (for c in trials: ... trialnum = len(c)) so I can store the trial number along with the response, I get a message about it not being iterable in this form. Basically I feel that these two issues are related in some fundamental way.

Any help would be appreciated

Thanks!


Solution

  • It's because random.shuffle shuffles in place and returns None (that's why you get an error about NoneType), so do

    random.shuffle(pairs)
    

    instead of

    pairs = random.shuffle(pairs)
    

    As a general comment, you would not generate a lot of TextStims but rather generate one and then update that when you run the experiment. It looks like you're doing a Stroop experiment or something like it. So do something like this:

    # General setup
    import random
    from psychopy import visual, event
    win = visual.Window()
    
    # A TextStim and five of each word-color pairs
    stim = visual.TextStim(win)
    pairs = 5 * [('blue', 'blue'), ('red', 'blue'), ('green', 'yellow'), ('red','red')]
    random.shuffle(pairs)
    
    # Loop through these pairs
    for pair in pairs:
        # Set text and color
        stim.text = pair[0]
        stim.color = pair[1]
    
        # Show it and wait for answer
        stim.draw()
        win.flip()
        event.waitKeys()