psychopy.org | Reference | Downloads | Github

Repeat loop if incorrect

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

OS (e.g. Win10): win10

**What are you trying to achieve?:
I’m trying to make a loop which repeats until the participants get all the questions in ‘check’ routine correct, which are composed of 4 random questions.
I’ve set nReps of trials_2 as 100, so that it ends before 100 whenever the participants get all right.

What I want is this.
i) If he/she gets perfectly right, she can move on to next routine.
ii) If he/she gets wrong for 1 or more questions, he/she has to move back to memorize_1 routine and study it again.

With current code, no matter I get right or wrong, it just moves to the next routine without repeating.

Flow and the code I inserted for ending trial is as below.


What did you try to make it work?:

Thanks for your help in advance.
You always help me a lot.

You’ve covered up the right side of the flow panel so it is hard for us to see if there are other routines beyond check within trials_2. Note that the .finished attribute does not take effect immediately: it is only evaluated after the last routine in a loop. So if there are other routines after check, they will still be executed unless you also insert some check code within them.

sorry for the bad capture image.
Actually, there’s no more routine after check.
That’s all!

OK, I see. Then I think the issue is simply that you’re trying to stop the wrong loop. You need to stop trials_5 first. Trying to stop trials_2 would have no effect until trials_5 is complete.

It helps to give your loops conceptual names rather than accept the defaults: this helps make it clearer in the code what is being attempted. e.g. call your outer loop trials and the inner loop checks. This helps your code become more readable, by using names that refer to your procedure rather than be arbitrary labels.

Thanks for your advice.
I changed to these codes, but now the problem is that it moves to the next routine after pressing any key to first question (out of four) in check routine. How should I solve this out?

if ‘0’ in [key_resp_5.corr]:
pass
else:
trials_5.finished = True
trials_2.finished = True

  • OK, I think you really might need to define exactly what your task involves here. I’m still not sure what the flow of the logic is supposed to be. e.g. it seems that you don’t want to end the outer loop upon someone making an error, but instead allow it to run again?
  • What do you mean by ‘next routine’? check? memorise_1? Or something beyond trials_2?
  • Please surround your code before an after by three backticks (i.e. ```) to make it readable and preserve indenting, which has meaning in Python.

The logic of your code is more than a little obfuscated. It could simply be:

if not key_resp5.corr:
    trials_5.finished = True

i.e. this would end the inner loop after any incorrect response, so the next iteration of the outer loop would occur. If the response was correct, no action would be taken, and the inner loop would simply iterate again.

If the responses don’t seem to be having the desired effect, you can check the relevant values with some temporary debugging code:

print(your_correct_response_variable_name)
print(key_resp5.keys)
print(key_resp5.corr)