Failed to implement conditional branch


PsychoPy version :v2021.2.3

My purpose: if the answer of question 1 in routine 1 is “2” (ans1_slider is “2”), then skip routine 2 in which is question 2.

What I have done:
Add a code component in routine 2 , the details are as follows:
In the Begin Experiment:


In the Begin Routine:

if ans1.response==2:

Each of the two routines has a loop, named as "trials_que"and “trails_que2”. I didn’t change much settings of the loop, just set the nReps to 1.

my problem:
Whatever my answer to question 1 is , question 2 always appears, which means it didn’t skip routine 2.

Hello Gloria,

do you assign the slider-answer (ans1_slider) to ans1.response?

Best wishes Jens

1 Like

Hello Gloria,

did you set the granularity of the slider to 1 (integer). The default is 0 or continuous. So your code might simply fail because the rating is never 2 but, for instance 1.999 or 2.001.

The following code skips the second routine (in the same loop). You might need to adopt the names:

if slider.rating == 2:
    continueRoutine = False

This assumes that you slider is named slider. This code is in a code-component of a routine directly following the routine with the slider. The code-component is in the top row and the code is in the Begin routine tab.

Best wishes Jens

1 Like

Thank you for taking time to answer my questions.I really appreciate your help!
Yes, I have set the granularity of the slider to 1. And the slider is named as ans1.
I’m not sure if I successfully assign the slider-answer to ans1.response. Is it enough to write the following?:


I did this because there is a column in the outcome file named ans1.response.

Sorry! I just figured out how to upload the screenshots.

I have also tried to put the two routines into one loop, but it won’t work either.

Thanks again for your reply!


what do you intend to achieve with?


Which routine do you want to end? If you want to end routine2 you need to put that code that evaluates the slider-response in the Begin routine tab of routine2. The slider ans1 is, I assume, in routine.

Notice that there is probably no need to assign the slider-response ans1 to a variable. You can access it directly with ans1.rating. So change your code to

if ans1.rating == 2:
    continueRoutine = False
    trials_que.finished = True

BTW, try to use names for your routines that make more sense than routine and routine2 :wink:

Best wishes Jens

1 Like

Thank you! It worked!
Really appreciate your help!!