| Reference | Downloads | Github

Multiplying slider.markerPos

Hi all,
I have one question related to the slider. I want to create a slider with tickmarks going from 0 to 10 (endpoints have labels). Clicking on the slider does not end the routine, because I want the testers to have room to tweak their ratings. I have a TextStim that shows the current value, by using a code component and adding text.text=slider.markerPos in the Each Frame box. However, I want this text to show integers ranging from 0 to 100. It seems like the easiest way to get the 0-100 scores while still keeping the 0-10 tickmarks is to make ticks range from 0 to 10, set the granularity to 0.1, and then multiply slider.markerPos by 10 for the TextStim. Is that possible? When I try, I get the error:

text.text = slider.markerPos*10
TypeError: unsupported operand type(s) for *: ‘NoneType’ and ‘int’

I know this can be done with rating scales, but this is for an online study, so it has to be the slider.
Any help would be appreciated!

What happens if you try just presenting the raw slider.markerPos ?

I think the problem is that slider.markerPos is undefined until it gets clicked so you should make sure that your line of code is only executed when **slider.getRating()!= None
** (change null to undefined in JS)

This leads me to suspect that you should be using slider.getRating. e.g.

if slider.getRating() != None:

Does that work locally? If so, it should translate to JS with the null=undefined as noted.

Best wishes,


Yes, that works! Thank you so much! I still get a .0 decimal (e.g., 12.0, 55.0, 73.0), but perhaps it’s unavoidable.

A quick follow-up question: I have a loop where a stimulus routine is first presented, and then a rating routine with the slider and the TextStim showing the slider’s value. The TextStim seems to remember the slider rating from the previous trial. Thus, you see a value on the screen before clicking on the slider in all trials except the first. Is it possible to reset the text in the next trial?

Thanks again!

You should be able to round() to remove the decimal and .setText(‘’) to remove the text.

The .setText("") works, but not the round() function. Where would you place that function? This does not work (no error, but still shows the .0 decimal):

if slider.getRating() != None:

Did you add round=Math.round; or

round = function(num, n=0) {    
    return +(Math.round(num + ("e+" + n))  + ("e-" + n));

to code_JS?

My code type is the “Auto->JS”, but I thought that the function was defined, because my Py code is automatically translated to (in the JS code):

if ((slider.getRating() !== null)) {
    text.setText(Math.round((slider.getRating() * 10)).toString());

If I copy and paste your function into the JS code, I still have the .0 decimal (no error message).

Please could you have a look at my crib sheet to see what I mean by code_JS

OK, so I have added a code component in my first routine, moved it to the top, switched from “Auto->JS” to JS, and copied and paste this into the “Begin Experiment” box:

round = function(num, n=0) {    
    return +(Math.round(num + ("e+" + n))  + ("e-" + n));

But I still get the .0 decimal. By the way, does it matter that every time I open code_JS, it has switched back to “Auto->JS”?

I’m sure I’ve come across this before but I can’t remember how I solved it. How about str(int(slider.getRating() * 10)) ?

Yes, that worked! thank you!

1 Like

BTW, it doesn’t matter that it defaults back to Auto so long as you don’t click on the Python window.