| Reference | Downloads | Github

Ending a repeating loop of trials with a single key press

OS Win10
PsychoPy version 3.1.5
**Standard Standalone? y
What are you trying to achieve?:

Hello everyone. I have tried to search for the answer to this question on forum, but to no avail. Apologies if this question really is answered elsewhere. My Python skills are minimal, but I think some coding is needed? I am looking to create a Change Blindness Task whereby an image (e.g. Image A) appears on screen for 0.5seconds, then we have 0.2 seconds of nothing (mask), and then a very slightly different version of the same image (i.e. Image B). Just one tiny change.

I wish to repeat this loop fifty times, before moving a text screen that gives participants a choice of options as to what they saw (i.e. 1 - did an object disappear; 2 - did an object change colour, 3- didn’t see any differences, etc.) This I can do, but I want the loop to end after fifty repititions (of both images) or when the participant spots the difference and presses the space bar. It is this last bit I can’t do. I think it requires some code, but I don’t know what to do.

What did you try to make it work?:

This is the basic set-up:

0.2 pause.
0.5 seconds of image.
0.2 secs of nothing.
0.5 seconds of image.

I just have this on loop with the images coming through correctly as signposted on the Excel file, etc.

What specifically went wrong when you tried that?:

I can’t end the loop prematurely when spotting the difference by pressing the space bar. I think it might be just pushing PsychoPy onto the next image in the cycle, and not to the question (Query_P1). For example, rather than:

’Image B_32nd time’ + ‘space bar’ => 'query_P1’

I think I’m getting:

’Image B_32nd time’ + ‘space bar’ => ‘Image A_33rd time’.

I hope that makes sense. There is no error message or anything. The loop just continues to run until the iterations end and I cannot artifically end it by way of a key press.

Many thanks in advance for anyone’s valuable time. It’s a cracking piece of kit if I can just figure this bit out.

Hi @James_Jackson, some clarification on the design would be useful. Do you want a question to follow on from every pair of pictures? So one iteration of a trial might look like:

for 50 trials

  • present base image
  • present image with change
  • present question


I wasn’t entirely sure either, but I think you might need something like this in the Each Frame tab of a code component:

if key_Prac1.keys == "space": 
    continueRoutine = False 
    loopP1.finished = True 

However, I wanted to point out something else: If I understand your design correctly, I think you want your keyboard component to start at time 0. This would allow participants to respond at any time during the trial. Currently, you allow them to respond between PracA and PracB, but not between PracB and PracA.


Thanks @dvbridges and @jderrfuss.

Sorry I was not clear enough. It’s:

1a) present base image
1b) present image with slight change

2a) present base image
2b) present image with slight change

3a) present base image
3b) present image with slight change

And so on and so on, but to stop and move to the question screen after: (i) fifty loops or (ii) a keypress.

And @jderrfuss - yes, an excellent point. I think I do want my keyboard component to start at time 0. Excellent catch. I will try that code example when I can, and will report back.

Further suggestions gratefully received.

Best wishes all,

Hi everyone,

I thought I would take the opportunity to confirm that I figured it out in the end, and all with the Builder and no need to use Coder. The issue was to have a simple pair of images displayed, then for the basic structure to loop fifty times. Try as I might, the looping caused a lot of problems. One issue I could not get round was the fact that the keyboard reponse also looped. So if my loop lasts 1.2 seconds, and someone presses the space bar opn the 37th loop (i.e. between 43.2 and 44.4 seconds into the task), the time recored would only be between 0.0 and 1.2 seconds.

In the end I brute forced it and my routines each contain 100 components (image A, image B, image A) etc. This has the advantage of being very simple and needs only one keyboard component, but it is big, took time to construct, and there is quite a hang (up to one minute) before the task begins. But when it does, it works like clockwork.

There are probably better methods but:

(a) I managed it
(b) The concept is very simple (if time-consuming)

Best wishes and thank you to @dvbridges and @jderrfuss for their comments.

Hi, James,I met the a problem similaried to yours.I need to press ‘e’ to end the loop,but it desn’t work.My code end the routine is this:

I don’t know how to fix it.Can you tell me more detail about your method?

HI @Melody, you have a typo on the code above. The word finishd should be replaced with finished.

Oh, that is! Thanks a lot .It is one of the problems that troubles me quite a few days. How silly I am and you are really helpful! @dvbridges

1 Like