Howdy, Stranger!

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

Supported by

[solved] Displaying an inline-generated variable in the sketchpad.

edited March 2016 in OpenSesame

Hi.
I'm having a very hard time doing what I thought would be a rather simple task using OpenSesame.

I'm trying to generate a random number (using the inline script) and display it on the screen using the sketchpad.

my humble script:

import random

beg = 10
end = 19
leftnum = random.randint(beg,end)
r = range(beg,leftnum) + range(leftnum+1, end)
rightnum = random.choice(r) 

This actually generates two numbers (rightnum and leftnum), and now I want to display these two variables/numbers using the sketchpad.

Simply creating a text object and filling it with [leftnum] or [rightnum] doesn't work.
calling them var.leftnum and var.rightnum inside the script also fails.

Is there any simple way to do this?

Thanks for the help.

Comments

  • edited 8:13AM

    Hi Zennie,

    You're almost there! In order to use a variable outside of the inline_script, you have to make it 'available' elsewhere in your experiment, by means of exp.variable = 'your_number', or exp.set('number', str(leftnum)+str(rightnum)). In your sketchpad you should then be able to use [number].

    Cheers,

    Josh

  • edited March 2016

    Thanks for the help, but It's still not working.

    Just to make sure I follow you:
    I have added

    exp.variable = 'leftnum'
    

    to the end of my inline script.
    Now in the sketchpad I use a textline element where I insert [leftnum] as the text.

    Running the experiment, I get the error: "The variable 'leftnum' does not exist. Tip: Use the variable inspector (Ctrl+I) to see all variables."

    Am I doing something wrong?

    BTW, the sketchpad is located just after the inline script, under the same sequence.

  • edited 8:13AM

    Solved it.
    Thanks to Josh who helped.

    All I needed to do after setting the variable was to move the entire code to the prepare tab (and not the run tab) in the inline script.

    Apparently, the sketchpad is getting ready before the inline code is running, so it does not get the new variable I defined.

  • edited 8:13AM

    Hi Zennie,

    Great that you've solved it. Just to avoid problems in the future: if you create a variable like this exp.variable = 'leftnum', you'll end up with a variable that literally contains a string 'leftnum'. I gave the example exp.variable = 'your_number', but that is assuming you already have a concrete number like '56'. You created a variable leftnum that contains the number, hence, you want to use exp.variable = leftnum, so that your new variable uses the value that leftnum refers to, rather than the string 'leftnum'.

    Cheers

    Josh

Sign In or Register to comment.