| Reference | Downloads | Github

Repeat trial if answers are incorrect

Hello! I’m trying to build a Go/No-Go experiment in PsychoPy, and I need help on one particular part of the experiment:

The first routine is a trial routine (to make sure the participant understood the instructions, before he/she moves on to the main routine). What I’d like is to repeat the trial routine if the person doesn’t get 75% of correct answers. If he/she does 75% (or more) correct, he/she can move on to the next routine.

I’m kinda new in this world and I don’t understand anything of programming, but I can see that I will have to use the code component. Can you help me with that?

Thank you!

What you probably need to do here is have a nested loop structure. i.e. once the inner loop is finished, you check what the percentage correct was. If it is < 75%, then do nothing, and the outer loop will run again, giving another practice session. If it is > 75%, then you terminate the outer loop so that experiment will proceed to the next step.

Try searching in the forum for setting .finished = True: to end a loop:

But how do I check the percentage of correct answers?

I understand I must do something like “if the percentage of correct answers is below 75%, repeat loop” and “if the percentage of correct answers is above 75%, continue to next routine”, but I don’t know how to say that… I guess there must be some kind of formula (?) to calculate that, but I’m having a hard time trying to figure that out…

This is how my data appears, so I don’t know how to calculate the correct answers…

Try looking here:

I used the code component you reccomended in that topic and it worked. (Thank you!)

However, when the answers are correct, it keeps on repeating the outer loop… What can I do to stop it, and move on to the other routine?

We’ll probably need to see you actual code snippets, and a precise description of what is needed. “when the answers are correct, it keeps on repeating the outer loop” doesn’t tell us too much since we don’t know your exact requirements. Let us know exactly what should happen and when.

I built a Go/No-Go experiment using an excel file:

The Go stimulus is the green circle; when it appears the person should press the space bar. Whenever the other images appear (green square and red circle), the person is not suppose to press any button.

My experiment is divided in two parts: the first one is a trial routine, to make sure the person understood the instructions. However, the person has to get 75% correct answers, so that he/she can move on to the next routine, which is the one I’m actually evaluating.

My problem is that I don’t know how to accomplish that. I searched some topics in the forum, and the only thing I understood is that I will need a code component to do that.

I added that outer loop like you said:
" 1. Two routines: one for the instructions, followed by one for the practice task.
2. Two loops: an inner one that surrounds just the second routine, and an outer one that encompasses both routines (so that the inner loop is nested within it)."

Now, when the trial is over it goes back to the instructions routine. However, I need to know how to move on to the next routine, when I get 75% correct answers.

I don’t know if I could explain myself better this time… Thank you anyway!

I understood what you said on that topic about doing two routines (one for the instructions and another for the trial), and two loops.

However, I think the coding isn’t working, or there’s something that I did wrong, because it keeps repeating the outer loop. And I want to move on to the next routine, once people get 75% correct answers.

I’m sorry for bothering you, but I honestly don’t understand coding… If you could help me, it would be very important!

It looks like you have everything set up correctly and the code looks good. It should do what you want. And yet it doesn’t. Don’t worry: this is a state in which all computer programmers spend a good chunk of their time!

My gut feeling, since the code looks OK, is that the problem might be with the keyboard component deciding if the response is correct. But we really can’t tell at this stage.
I would suggest that you sprinkle your code with some print statements so that you can debug the code by following its progress on every trial.


if key_resp_5.corr:
    number_correct = number_correct + 1
    print('Correct: ' + str(number_correct))
    print('Not correct')

if trials_3.thisN == trials_3.nTotal:
    if number_correct/(trials_3.nTotal + 1) >= 0.75:
        trials_4.finished = True
        print('Score: ' + str(number_correct/(trials_3.nTotal + 1)))
    print('Trial: ' + str(trials_3.thisN))

This will hopefully show you what is going wrong and when. Once the problem is fixed, remove all of these extra print statements.

Let us know how you get on.

It doesn’t allow me to run the experiment…! I think I did exactly what you told me…

The brackets don’t match. Change the line:

print('Score: ' + str(number_correct/(trials_3.nTotal + 1))

to be:

print('Score: ' + str(number_correct/(trials_3.nTotal + 1)))

This is what I get at the end:

I only got one wrong answer, and it still didn’t move on to the next routine… I don’t know what to do…

So that debugging output tells us that this line:

if trials_3.thisN == trials_3.nTotal:

never evaluates to True, as you never see output of either 'Finished.' or 'Score: '.

My guess would be that trials_3.nTotal is 1-based (i.e. the actual number of trials), whereas trials_3.thisN is 0-based. Hence it is possible that on the last trial, trials_3.thisN falls short of trials_3.nTotal by 1. Hence, the loop-ending code would never get executed.

You can (and should) test this by adding some print statements. If it is the issue, it would be fixed by something like this:

if trials_3.thisN + 1 == trials_3.nTotal:

It worked!!! It’s working perfectly!!! Thank you so much, I really appreciate your help!