| Reference | Downloads | Github

Conditional repetition of a routine depending on key pressnot working

I’m using Psychopy3 v2020.2.10 and I want to add an option that participants can either view the instructions again, or continue to the real experiment, depending on whether they press ‘y’ or ‘n’. The previous advice I’ve seen on this forum for a similar thing, for example here and here , still doesn’t work - specifically, the screen always freezes and no key responses change anything.

Here’s a copy of my experiment - the relevant code to end the instructions is code_2 in the routine ‘Instructions3’._tsl_fisher_4.psyexp (50.1 KB)

Thank you :slight_smile:

You need to tick Force end of routine in key_resp_2.

You could also restrict the allowed keys to ‘y’,‘n’ but that shouldn’t matter.

Thank you! So clicking Force end of routine allows me to return to the beginning of the instructions loop, which is good for when they want to view the instructions again.
But I need to also add another option which allows the whole loop to end, i.e. ‘instructionz_loop.finished = True’, which happens if they press a different key. I think that clicking ‘force end of routine’ means that whatever key is pressed, it returns to the start of the loop, rather than one key doing this, and the other allowing the loop to be left?

You code in End Routine should have worked. Please could you show it just in case I missed something obvious.

I put this in endroutine of the final routine:

if len(_key_resp_repeatz_allKeys):
key_resp_repeatz.keys = _key_respz_repeatz_allKeys[-1].name # just the last key pressed
key_resp_repeatz.rt = _key_respz_repeatz_allKeys[-1].rt
# a response ends the routine
if key_resp_repeatz.keys == ‘y’:
instructionz_loop.finished = True;
elif key_resp_repeatz.keys == ‘n’
continueRoutine = False;

I also tried putting it in EndFrame but that didn’t work either…

I actually also tried another method which I found here but this didn’t work either. Have other people been able to get conditional key responses to work in Pavlovia from Psychopy2020.2.10?

Thanks so much :slight_smile:

That doesn’t look anything like the simpler code I saw when I downloaded your psyexp file earlier. You seem to have changed the name of the keyboard component and added code to check for a response when there has to be a response to end the routine.

Before you had:

if 'y' in key_resp_2.keys:
    instructionz_loop.finished = True;

However, that has a stray semi-colon. It should be just True in Python, though it looks like it translated okay despite this.

By EndFrame do you mean Each Frame?

Sorry, yes I mean each frame.
Thank you for your help. For some reason I couldn’t get it to work by doing that. However, I’ve found a way which is based on something here which involved re-coding the ‘y’ response as ‘correct’, and then inserting a new routine which sets the number of nReps depending on whether the response was correct or not. I’m not sure what I was doing wrong before!