How we can put a python function into a variable?

Hi everyone. I’ve created an experiment in psychopy using the builder view and I’ve added some code components inside it. The code component contains some functions and it actually works well but in order to put it online I need to put functions into a variable.
I’m a kind of beginner to python and programming and I tried to do what I want but after putting function into a variable it doesn’t do its task properly.
This is a piece of my code:

def num1_to_sec(n):
    return float(0.250 * n)

def num2_to_sec(n):
    return float(0.270 * n)

rann=randint(0,1)
def stopSDVIS(rann):
    if rann==0:
        return num1_to_sec(listSD[a][b][c])
    elif rann==1:
        return num2_to_sec(listSD[a][b][c])

The function I need to put into a variable is stopSDVIS(rann). I tried these three ways:
stopvar=stopSDVIS(rann)
stopvar=stopSDVIS
stopvar=stopSDVIS()
They don’t end up in error but all these three ways produce incorrect outcomes.
So in a nutshell I want to know how to put my python function into a variable.
Thanks in advance for your help.

Python as a language doesn’t run within a browser, so to run your experiment online, you will need to translate your Python code into JavaScript (which all browsers can run).

You can get a start on this by using PsychoPy’s built -in Python to JavaScript translation. Do this by selecting the “Auto -> JS” option in your code components. You then might need to switch to “Both” so that you can manually edit the automatically-translated JavaScript, as it doesn’t work miracles.

Besides @Michael’s suggestion to use Auto>Js as a base to then do manually translation from, you may be referring to this post about custom functions in js, that might help

1 Like