psychopy.org | Reference | Downloads | Github

One line if statement (tenary operator) for component duration

This is not really a question. I just wanted to post my finding here in case someone else has the same issue or in case this is a bug.

I’m using Psychopy v2020.2.3 (as per psychopy.__version__ , although I installed version v2021.1.3 from the Github releases page).

In most Builder entry fields, you can use a tenary operator. For example in the “selected rows” field for one of my loops I put:

$f'0:{my_n_trials}' if halves.thisRepN == 0 else f'{my_n_trials}:-1'

And that worked like a charm.

However, if you use a tenary operator in the durations field of a component, for example a Textbox, that has an unexpected effect:

$240 if not expInfo['debug'] else 10

If you compile the script, you’ll see why. There is this if statment that determines if the component is finished:

if tThisFlipGlobal > pause_textbox.tStartRefresh + 240 if not expInfo['debug'] else 10-frameTolerance:

Here, if expInfo[‘debug’] is True, the statement is evaluated as:

if tThisFlipGlobal > 10-frameTolerance:

… and this will be true as soon as you’re 10 seconds into your experiment.

The issue can be resolved by putting brackets around your code:

$(240 if not expInfo['debug'] else 10)