Syntax error on valid python code [builder]

Hello,

I am working in builder and hoping to do a reverse dictionary lookup so i added

all_keys = [key for key, value in action_transitions.items() if value == end_island]

alternatively

key = next(key for key, value in action_transitions.items() if value == end_island)

(note- action_transitions = dictionary , end_island=item)

These both work perfectly well in python but in the code segment of builder i see a

/* Syntax Error: Fix Python code */

anyone know what could be causing this?

Thanks in advance

Do you want to run this experiment online?

The syntax error means that your code can’t be translated into PsychoJS. I find this useful for spotting simple errors in my Python code. However, if you know why you are getting the syntax error (reverse dictionary lookup) and don’t need it to work online, then don’t worry about it.

I do want to run the experiment online. I guess I would need to re-forumate the reverse dictionary lookup in a way suitable for PsychoJS?

1 Like

Hello,

PsychoPy apparently does not auto-translate a brute-force approach of a reverse dictionary lookup

my_dict = {"color": "red", "width": 17, "height": 19}
value_to_find = "red"
for key, value in my_dict.items():
    if value == value_to_find:
        print(f'{key}: {value}')

(taken form here) The above code gives a JavaScript syntax error (auto-translate apparently throws an error on

for key, value in my_dict.items():

So you need a code-component that is set to Both instead of Auto → JS and provide separate lookups-routines on each side. JS-solutions can easily be found.

Best wishes Jens