Pick up a randomly chosen value from previous routine

OS (e.g. Win10): MacOS Big Sur Version 11.6.1
PsychoPy version (e.g. 1.84.x): v2023.2.3
Standard Standalone? (y/n) If not then what?: y
**What are you trying to achieve?:pick up a randomly chosen value from previous routine in a subsequent routine

What did you try to make it work?:
In the routine Instr_Navon I used this code in the Begin Routine:

choix = [1, 2]
shuffle(choix)
thisExp.addData('Choix_du_message', choix[0])

In a later routine called induction_style, I used this code to pick up the chosen value from Instr_Navon and condition rows from the Excel file to use in induction_style:

choix_stored = thisExp.getNewestTrial().data['Choix_du_message']

if choix_stored == 1:
    useRows = ['8:15']
else:
    useRows = ['0:7']

What specifically went wrong when you tried that?:

The experimend crashed when starting the induction_style routine. I got this error message:

choix_stored = thisExp.getNewestTrial().data[‘Choix_du_message’]
AttributeError: ‘ExperimentHandler’ object has no attribute ‘getNewestTrial’

Why not reuse choix[0] ?

Okay, why not. I’m not sure how to modify the code though, can you help me?

if choix[0] == 1:
    useRows = ['8:16'] # second 8 rows
else:
    useRows = ['0:8'] # first 8 rows

Oh yes I tried that even before. But as “choix” is defined within the prior routine (“Instr_Navon”), the new routine (“induction_styles”) won’t recognize it.

It should recognise it unless you are resetting/clearing the value manually.

Also, you could set useRows in the earlier routine if it only depends on choix.

If I use choix in the new routine it just runs all rows from the excel file. I’m not sure how useRows can be used in the earlier routine when the earlier routine is not in the loop linked to the Excel file.

useRows needs to be set BEFORE the loop starts