Branching Loop Not Working Properly

Hi,
I am trying to branch out my routines so that certain routines are presented depending on a participants’ responses to a text box. If participants enter “1”, “2”, “3”, “4”, “5”, or “6” in the text box, then the first routine loop should show and skip the second. If participants enter “x”, then the first routine loop should skip, and the second should be presented. Currently, the study is skipping the first routine and instead presenting the second routine in all cases (i.e. when I enter “1”, “2”, “3”, “4”, “5”, or “6” or “x”).

Here is my flow:


I added this code to the routine where the text box is located:

The “DoTradConfID” and “DoTradConfReject” are listed as variables for their respective loop:

I followed these instructions but am still running into problems:

Thank you!

Hello Eyewitness,

I would say that your if-construction is wrong. Your intention to test for 1, 2, 3, 4, 5, or 6 does not translate to
if (LineupDecisionTextBox.text == '1','2','3','4','5','6'):

you need to specify that you want to test for 1, 2, 3, 4, 5, or 6 see here for logical operators in python. If you define correct answers in your .xlxs/.csv file, you could use a keyboard-component and test the answer for correctness.

if key_resp.cor:
    do_something
else:
   do_something_else

Best wishes Jens

1 Like

Hello! Thank you for your reply. I am wanting a loop to show up or be skipped depending on the participant’s decision (expressed by 1-6 or X). So, I am not interested in whether the participant is correct, though I will keep your suggestion in mind for future studies!

However, I did try to modify my if-construction. First I tried adding ‘or’ in between each number (i.e. ‘1’, or ‘2’, or ‘3’…) instead of listing them as I do in line 1. However, this code was not correct (it showed an error). Next I tried to replicate lines 1-7 for each separate outcome rather than listing all the numbers and letters together… But I still have the same problem where the first loop skips for all outcomes (i.e. when 1, 2, 3, 4, 5, 6 and x are selected).

Any additional suggestions are very much appreciated!

Please could you show your conditional code?

I think you may be getting the syntax wrong.

You should write if A == 1 or A == 2 not if A == 1 or 2

Hello, please see below. I tried to incorporate your suggestion but I may still be doing this wrong because the same issue is still occuring.

if (LineupDecisionTextBox.text == '1' or LineupDecisionTextBox.text == '2' or LineupDecisionTextBox.text == '3' or LineupDecisionTextBox.text == '4' or LineupDecisionTextBox.text == '5' or LineupDecisionTextBox.text == '6'):
            DoTradConfID=1
            DoTradConfReject=0
else:
       DoTradConfID=0
       DoTradConfReject=1
if (LineupDecisionTextBox.text == 'x' or LineupDecisionTextBox.text == 'X'):
            DoTradConfID=0
            DoTradConfReject=1
else:
       DoTradConfID=0
       DoTradConfReject=1

Thank you!

Try

if (LineupDecisionTextBox.text == '1' or LineupDecisionTextBox.text == '2' or LineupDecisionTextBox.text == '3' or LineupDecisionTextBox.text == '4' or LineupDecisionTextBox.text == '5' or LineupDecisionTextBox.text == '6'):
            DoTradConfID=1
            DoTradConfReject=0
elif (LineupDecisionTextBox.text == 'x' or LineupDecisionTextBox.text == 'X'):
            DoTradConfID=0
            DoTradConfReject=1
else:
       DoTradConfID=0
       DoTradConfReject=1

Thank you! I tried that code and it still did not work… It is still skipping to the DoTradConfReject no matter the response. Would any other code components in the same routine be interfering with this code? For example, I have code entered to restrict the keyboard to just 1-6 and ‘x’ and I also have another code which forces text entry before the participant can move onto the next screen. I do not think these would affect other codes but wanted to make you aware of them just in case.

Try print('LineupDecisionTextBox.text',LineupDecisionTextBox.text) at the top to see what’s going on.

Is this in Begin Routine? I think that it needs to be in End Routine of the routine with the editable text component (above the text component).

Yes, this code is in the End Routine section. I also moved the editable text component above the text component in Builder and the problem still persists… I am wondering if I should replace the textbox with a text component and enter code so that the participant’s response shows up on the screen. Could this be a problem with the textbox component? Thank you for your help!

Hello! Thank you for your help! Just to provide an update… I replaced the text box component with a keyboard response and that seemed to work just fine. The issue now is that the logic won’t work in Pavlovia even though it worked in psychopy. Here is the link to the forum post that contains the code that worked in Psychopy (but not pavlova) in case this is useful to any reader! (Branching Logic working for Psychopy but not Pavlovia)