Loop control with keyboard

Hi everyone, i work on the latest version of PsychoPy : 2023 2.3 on Mac OS

I don’t know if there was an article about it, but if it was I’m sorry, I didn’t find it.

What are you trying to achieve?:

I aim to control “a practice loop” to help my patient understand the real test.
So if I start the practice loop and they understand the objective of the loop I want to “break, exit” with a specific key on the keyboard. But if they didn’t I want to reset/restart a loop when I press on another key.

Since I am a beginner in coding, I don’t get the problem of the multiple codes I have tried for the past few days.
What did you try to make it work?:

My latest attendance is this :

What specifically went wrong when you tried that?:

The loop doesn’t restart when I press y, I don’t know if it’s linked to the number of repetitions.

I would be grateful for any advice:smile:

Hello

A loop will only iterate as many times as specified. You can’t restart it. One way is to specify a very large number of iterations and exit the loop when a criterion is met. Another way is to specify a second surrounding loop with a sufficient number of iterations, and break out of this loop when the criterion is met.

Best wishes Jens

1 Like

Can I have like an example or demonstration cause I don’t think i get it :confused:

loop.finished = False doesn’t do anything, because that’s re default behaviour.

What do you want to happen if they don’t press lshift or y?

Why are you using two different keyboard components for those two keypresses?

I was using 2 different keyboard components cause I thought the code wasn’t taking the fact that key_resp had: lshift and y.

When they press lshift I want to exist the loop and go to pas_bjr and when I press y, the loop restarts itself.

In that case put y as a possible response for key_resp.keys, put the nReps for loop at 999 and delete rows 4-5 of your code.

Just tried it, but the loop will restart itself even when I press lshift, and won’t go to the other routine pas_bjr

I tried different codes for the past few days, feel like it doesnt take into account the key_resp in it.

What tab is your code in? If key_resp ends the routine then you don’t need line 2 and the code should be in End Routine.

Hello,

Try this

BreakLoop.psyexp (15.1 KB)

I hope I achieved the intended behavior using @wakecarter’s suggestion.

Best wishes Jens

Just tried it, and works thank you so much !!!