psychopy.org | Reference | Downloads | Github

Ending loop conditionnally. While loop?

Hello,

I’m building a program in which participants should select a certain stimulus that appears on the screen (Imported images & text).
I want to end the loop if the participant clicks on the wrong answer.
The flow goes: selection, answer, rate, answer2, rate2.
I would like to stop the loop from moving forward if the wrong selection is made (ie: choose green instead of red).
I found the following code online:

while True:
    n = raw_input("Please enter 'hello':")
    if n.strip() == 'hello':
        break

However, I’m not sure how to make use of it or if I can make use of it in the builder.
Thanks in advance.

OS : windows 7
PsychoPy version : 1.84.2

No, that code is “raw Python” and wouldn’t work well in the PsychoPy environment. e.g.

  • raw_input() is designed to just get simple keyboard responses when Python scripts are run from the command line and people automatically get to see what they are typing directly.

  • In PsychoPy, we use graphical windows and only show people what they type if we choose to do so. Also, we can’t use infinite loops like while True: in Builder, because Builder is predicated on a cycle of drawing to the screen on every screen refresh (typically 60 times a second), so we can’t use code that will take an indefinite pause.

  • This code is trying to get a keyboard response, whereas you want to click on stimulus with a mouse.

The good news is that this is a common request. Look at previous questions on this forum and see if they help:

https://discourse.psychopy.org/search?q=mouse%20select

Hi Micheal,

Thanks for your answer. I didn’t find what I’m looking for in the forum.
The mouse works fine. I just want to stop/restart the selection routine when the wrong stimulus is chosen.
What I’m looking for is close to the following post:

Well… not all of it. My program is done except for the rejecting wrong answer part.
In which you explain:

OK. So stick with the experimental routine as described but then create two additional routines to follow it: one for error feedback and the other for your ratings and so on. You can select which one will be executed on each trial by inserting a code component and in the Begin Routine tab, putting some code to not continue with the routine, depending on the decision made. You then also need to wrap all three routines in another loop (i.e. nested inside your main loop). This loop isn’t linked to a conditions file. Just give it a large number of repetitions. i.e. this just makes the trial repeat until the correct selection is made. In the begin routine tabs, put something like this:

# for the error feedback routine:
if image_chosen == CorrectAns:
    continueRoutine = False # don't give error feedback for a correct trial

# for the rating routine:
if image_chosen != CorrectAns:
    continueRoutine = False # don't do the rating
else: # will do the rating,
    your_inner_loop_name.finished = True # and end the cycle of repeating this trial

For now my program doesn’t react when the wrong answer is selected. But I want an error feedback that goes with it.
I tried feedback msg = ’ ’ in each frame with a text component in the routine.

if image_chosen == CorrAnsV:
                thisExp.addData('correct', 'True')
                continueRoutine = False
        else:
                thisExp.addData('correct', 'False')
                msg = 'Error'
                continueRoutine = True
        break 

But no message shows. When I try it in the begin experiment tab the error message shows at all times.
I’m not sure where to go from here.