Changing rule when criterion is met (Wisconsin card sorting task)

I am new to psychopy and I have too little time to build this experiment.
I am creating a Wisconsin Card Sorting Task.
The WCST consists of two card packs having four stimulus cards and 64 response cards in each.
The WCST task is to classify cards, which differ by three criteria: color, shape, or number of the designs on the face of the cards, with the experimenter changing the criterion used after the participant makes 10 consecutive correct classifications.
The experiment ends when participants correctly classify cards by 6 criteria in this order: color, shape, number, color, shape, number or when participants finish the card packs (two packs of 64 cards).

Question 1): I created three loops: color, shape and number. How can I make the program go to the following loop after 10 consecutive correct responses?

Question 2): How can I make the program end the experiment when a participant completes 6 correct criteria (10 color, 10 shape, 10 number, 10 color, 10 shape, 10 number) or finishes the two card packs (128 cards, 64+64 cards)?

Thank you for your help:)

Hi Rachele,

for both question you must insert a code component and write a custom piece of code in python (place the code component at the very top of the component list!)

  1. In the feedback routine of each loop add something like this:
#in the begin experiment tab
color.counter = 0 

#in the begin routine tab
if key_resp.corr:
    color.counter = color.counter + 1
 if color.counter >= 10: #when 10 correct responses are detected
    color.finished = True #end the color loop. Change the name for each loop!
  1. This method should work if you want to end the experiment after 10 correct trial for each category twice, just have the “trials” loop be set to 2 repetions. For the 128 criteria you could add another counter in each trial:
#in the begin experiment tab
card.counter = 0 

#in the begin routine tab
card.counter = card.counter +1
 if card.counter >= 128: #when 128 card are picked end the experiment (I'am not sure about this part, so take it with a grain of salt!)
    continueRoutine = False
    thisExp.saveAsWideText(filename+'.csv', delim='auto')


Hi @tandy, that would work to keep track of ten correct responses, but what the OP wants is 10 consecutive correct responses. So the logic would need to be extended a bit. Would suggest a code component be inserted on the trial routine, with something like this in the “begin routine” tab:

if currentLoop.thisN == 0: # reset on the first trial
    n_consecutive = 0

Then in the “End routine” tab, something like:

if key_resp.corr:
    n_consecutive = n_consecutive + 1
    if n_consecutive == 10:
        currentLoop.finished = True
    n_consecutive = 0

# Save this in the data to check things are working OK:
thisExp.addData('n_consecutive', n_consecutive)

By using Builder’s currentLoop alias, rather than the hardwired name of a loop (e.g. color), the OP can re-use the same routine in each of the three loops.

NB @Rachele, the suggestion above should be able to end the loop when 10 consecutive correct decisions are made. But the loop will also terminate when it gets to the last trial, regardless of how many correct decisions have been made at that point, and go on to the next loop. What do you want to happen in that situation (i.e. if the subject never makes 10 consecutive correct decisions)?

Hi Michael, thank you for your answer. It works till the beginning of the last loop.
10 consecutive correct decisions in the first trial, 10 consecutive correct decisions in the second trial and then it stops.

If the subject never makes 10 consecutive correct decisions, he must go ahead making decisions till the 128 cards finish (for example, he can be stuck in the first trial for all the duration of the experiment). The experiment ends either if the 128 cards finish or if the participant makes 10 consecutive correct decisions in 6 trials (color, shape, number, color, shape, number…in this order), so I set 2 in the $nReps of the blocks and a sequential loopType.

Sorry, missed the “consecutive part” :sweat_smile:


Thank you very much Tandy for your help. I added the consecutive part :slight_smile:
Otherwise, the second part doesn’t work. I can’t let experiment finish when 128 cards are set.
Any advise?

I’m note sure,

for starter try to write a piece of code that DETECTS when 128 cards are drawn: you can have a small textbox in the corner of the screen that displays the total number of drawn card for example, that updates after each new card is drawn (so start at 0 and goes up by 1 every new card). If this works, then you can worry about ending the experiment after 128 cards :sweat_smile:


I see from your screenshot that you now have three different trial routines, rather than re-using the same one as in your first screenshot. This will mean that you will have had to copy the code snippets into a code component on each of those three routines. My guess is that the “begin routine” code on the last routine is broken (or missing altogether), meaning that the consecutive counter is not being reset at the beginning of that block, meaning that it will end immediately.

But that is just a guess.

Hi everybody! My experiment now works perfectly in Psychopy.
The only thing is that, on Pavlovia, the code for the changing rule doesn’t work till the participant makes an error (in the output file those responses are called NaN) and then it starts to work perfectly. Any advice??

I put codes here so If anybody needs a Wisconsin Card Sorting Task, here is all the code needed.
In the Begin Experiment: key_resp.counter=0

Hi Rachele,
Could you share your experiment in Pavlovia, please?
Your code was very helpful (thanks!), but I would like to see your complete experiment :slight_smile:
Thanks in advance!

Hi, I am a beginner of PsychoPy and I don’t have any experience with coding and programming. I also have little time to create WCST and have came crossed lots of problems when I building the experiment. These questions are very basic but I don’t know the answers. The first question is that whether I need to create stimuli by myself or I can import images as stimuli. The second question is that how to set the code so that the participant will be told that they have selected the wrong card.

Thank you in advance! It will be really appreciated if anyone can answer my questions!:)))

Hi, Rachele! I have a simple question to ask. How to set the target card so that it could include all 64 cards? For image button, it can only tag one picture at a time. Could you help me solve this problem? Thank you in advance!

Hi Rachele,

my program is not changing the rule. Can you please show me the details in the loops?


Hello eveyone,

my task is finishing when I made 10 consecutive correct decisions in 6 trials (color, shape, number, color, shape, number…in this order); however, when I tried to go ahead making decisions till the 128 cards finish, the task ended before showing the message:

AttributeError: ‘TrialHandler’ object has no attribute ‘abort’
################ Experiment ended with exit code 1 [pid:15508]

Do you know how to solve this issue?