Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Supported by

how to make variable's name dynamic in exp.pool[] box - inline script

edited May 7 in OpenSesame

Hi there,

I was searching answer for whole day, but can't find one, almost give in.

Basically, my question is:

In inline script (attached), I want make the var.xxx become dynamic (yes, works) and work as variable in exp.pool[] to refer different column names in the loop that list all the image names (this doesn't work :'( ).

for example:


i = random.randint(12,30)

var.xxx = u'var.bmi_F_' + str(i)

path = exp.pool[var.xxx]

face_canvas.image(path, x=0)

face_canvas.show()

while True:

r, timestamp = my_keyboard.get_key(keylist=[u'up', u'down', u'space'])

if r == u'up':

if i < 30:

i=i+1

var.xxx = u'var.bmi_F_' + str(i)

path = exp.pool[var.xxx]

face_canvas.image(path, x=0)

face_canvas.show()

elif r == u'down':

if i>12:

i=i-1

var.xxx = u'var.bmi_F_' + str(i)

path = exp.pool[var.xxx]

face_canvas.image(path, x=0)

face_canvas.show()

elif r == u"space":

break

log.write_vars() 



Thank you in advance! If there is any suggestion!

Chengyang

Comments

  • Hi Chengyang,

    Is there an error message? What exactly is the problem? Also, I am not sure what you mean with:

    work as variable in exp.pool[] to refer different column names in the loop

    Eduard

  • Have you found a solution? I'am also stuck in this.

  • Hi, to dynamically create a variable name, you can use the var.set function.

    For example, if you want to have a variable per trial, you could do something like this:

    var.set('my_var_{}'.format(trial_no),my_var_val) which would create a variable in the form of var.my_var_1, var.my_var_2, etc.

    See here: https://osdoc.cogsci.nl/3.2/manual/python/var/#function-var46set40var-val41


    Eduard

Sign In or Register to comment.