psychopy.org | Reference | Downloads | Github

Selecting one of the loops conditionally

Dear all
I use psychopy builder. The task is simple, participant hears a sentence and need to select one of the images on screen. Depends on the response the variables such as scores, incorr1, wrongAnswer are saved.
This part of the program runs perfectly.

As you can see I got three loops. I want to run either loop 2 (trials_3) or loop 3 (trials_2) based on one of the conditions which is met at the end of loop 1. I have included that in my code component (each frame) towards the end (the last four lines).

I struggled to make this happen. I understand that I need to do something with the last four lines of the code in the each frame section to do this.

I also understand that I need to define the loops upfront in beginning of the experiment. I am new to psychopy and any help is appreciated.

Thanks

Set some variables something like this:

if some_test:
    loop_2_reps = 10
    loop_3_reps = 0
else:
    loop_2_reps = 0
    loop_3_reps = 10

The put those variables (e.g. $loop_2_reps) in the nReps fields of their respective loop dialogs. Loops with an nReps of 0 won’t run.

1 Like

Thanks Michael

I added this in my Each Frame section after the condition I mentioned, and it seems to work perfectly now.

Thanks a lot. This is how it looks at the moment.

for stimulus in [image, image_2, image_3]:

    # check if the mouse is pressed within the current one:
    if mouse.isPressedIn(stimulus):

        # Yes, so store the reaction time in the data:

        # check if the stimulus' image filename matches the correct answer:
        if stimulus.image == eval(corrAns):
             thisExp.addData('score', 2)
             score = score + 2
        elif stimulus.image == eval(clCorrAns):
             thisExp.addData('score', 1)
             score = score + 1
             thisExp.addData('incorr1', 1)
             incorr1= incorr1 + 1
        elif stimulus.image == eval(incorr):
             thisExp.addData('wrongAnswer', 1)
             wrongAnswer = wrongAnswer + 1
        # end the trial once done here:
        continueRoutine = False 
      #  trials_5.finished = True
        if incorr1 >= 1 or wrongAnswer >= 1:
            loop_2_reps = 1
            loop_3_reps = 0
        elif score == 4:
            loop_2_reps = 0
            loop_3_reps = 1
        break

Hi Michael

Just another little tweak required, which I am not able to get done.

I need to add ‘+8’ to the score (I want it in the same column as ‘score’ in data sheet), if the loop_3_reps was run. Else, If the loop_2_reps was run, it would like the score to just be unaltered.



Any help is appreciated.

Thanks in advance.

I guess you could just add 8 to the score in the same section of code where you set the repetition number for loop 3 to be 1.

1 Like

Hi Michael

I added something like this, and it works. Thanks

        if incorr1 >= 1 or wrongAnswer >= 1:
            loop_2_reps = 1
            loop_3_reps = 0
        elif score == 4:
            loop_2_reps = 0
            loop_3_reps = 1
            thisExp.addData('score', 10)
            break