Skipping one loop or another

Experiment flow for reference:


Important code component:

What I’m trying to achieve: There is a Condition variable in the Experiment Info Dialogue box. This will be either 1 or 2 (no other possibilities). If Condition=1, the Reading_Easy loop needs to run once (the Reading_Difficult loop doesn’t run). If Condition=2, the Reading_Difficult loop runs once (the Reading_Easy loop doesn’t run).

What happens currently: Reading_Difficult loop runs once, no matter what value is entered in the Condition box. Reading_Easy loop never runs.

If I change the code to the opposite so it says:

if expInfo['Condition'] == 2:
    nRepsReading_Difficult = 1
    nRepsReading_Easy = 0
else:
    nRepsReading_Difficult = 0
    nRepsReading_Easy = 1

$nReps of Reading_Difficult loop is set to nRepsReading_Difficult
$nReps of Reading_Easy loop is set to nRepsReading_Easy

Then the opposite happens: Reading_Easy runs no matter what and Reading_Difficult doesn’t run.

Obviously something is missing here but I’m not sure what. Any help is appreciated!

Hi There,

What you want to do instead is end the routine and break the loop if the condition does not require that routine to occur.

In your “Reading_Task_Diff” routine, add a code component, in the “Begin Routine” tab add:

if expInfo['Condition'] == 1:
    continueRoutine = False #end the routine
    Reading_Difficult.finished = True #break the loop

In your “Reading_Easy” routine, do the same, but this time add:

if expInfo['Condition'] == 2:
    continueRoutine = False #end the routine
    Reading_Easy.finished = True #break the loop

This should skip the irrelevant routine dependant on the condition you entered in expInfo :slight_smile:

Hope this helps,
Becca

1 Like

Hi Becca,

I had tried something similar to that previously, and it didn’t do anything. When I tried it again, still nothing. When I run the experiment, both loops run no matter what number I enter into the condition box :confused:

I’m not sure if something funky is just going on or what because this makes sense to me and I feel like it should work…

Elisabeth

Hi - Please could you upload a copy of you .psyexp file so that I could take a look?

Thanks,
Becca

Sure, I’ve attached it here, thank you!
Exp_in_Development.psyexp (84.0 KB)

This copy has code in both places that are discussed above (just so you can see where they were).

Hi There,

OK I see the problem. Try changing your integer values to strings i.e.
rather than
if expInfo['Condition'] == 1:
try
if expInfo['Condition'] == '1':

Hope this works :slight_smile:
Becca

PS. for future when debugging these you can check a type by adding a code component and calling type on the variable you want to check. In this example print(type(expInfo['Condition'])) returns <class 'str'> showing this is a string rather than an int (whole number).

That did it, thank you!! :slight_smile:

1 Like