Preventing empty slider responses

PsychoPy version (e.g. 2022.1.1):

Hi, in my experiment I’m using the slider component and I’d like to prevent people from skipping the slider without giving a rating. I believe this can be done by setting condition for the slider component, but I’m not familiar with coding.
I’d appreciate your help!

On default, the routine ends when the participant presses a rating.
If you disable this option and have something like a key press to end the routine, you can do the following:

  1. Disable Force End of Routine in the slider component:

  2. Create a code component that ends the routine only when there’s a valid rating chosen, and then the ‘space’ button is being pressed:

# Every Frame:
if slider.getRating():
    if 'space' in event.getKeys():
         continueRoutine = False

Instead of a keypress, you can do the same with buttons, sliders, and any component you can think of. This is just one possible example.

Experiment Example:
prevent-empty-rating.psyexp (12.0 KB)

Hi Chen,

Thank you so much for your answer. I have another question if that’s OK.
With this code, the trial moves on to the next one as soon as a slider response is made. In the previous version of my experiment, I used the space bar to continue, but sometimes people press it twice accidentally.
I want people to be able to change their response, that’s why I need a keypress to move on to the next routine, but only let them move on if there’s a slider response.
Is it possible to adjust this code so that there’s a keyboard component to force end of routine, but only if there’s a slider response?

Hey,
My example does this precisely, without using a keyboard component but listening to events.
This code only works if you disable the Force End of Routine in the slider component. It works fine in the experiment I sent.

Explanation of the code
For every frame, check if there’s a rating. If there’s a rating, check if the ‘space’ key was pressed.
If ‘space’ was pressed, move to the next routine. Otherwise, don’t do anything.
Isn’t it exactly what you wanted?


You can do it with a keyboard component as well:

if slider.getRating():
    if 'space' in key_resp.getKeys():
         continueRoutine = False

image

Example:
prevent-empty-rating.psyexp (13.4 KB)

I wonder if this is happening because maybe we use different versions? For example, I don’t have that “register keypress on…” section. Because in the example psychopy files you sent me, the moment I click somewhere in the slider the routine ends. Force end of routine is disabled both in the slider and in the keyboard components. I can’t press space without rating, which is good, but I need to be able to press space after rating. However, it doesn’t give me the opportunity to do this and as soon as I click the slider it ends the routine.

This is odd. Did you try to test the experiment I sent? I want to make sure you’re referring to it.
Any chance you can upload your experiment here?

Yes, I refer to both of the experiments you sent. In both of them, I faced the same issue.
My experiment includes over 100 images so it may not be practical to upload it here. The only different thing I did for my experiment is to change the default slider and key_resp names to fit mine. But since I have this problem in an experiment that works for your PsychoPy makes me think it’s due to a version problem. In fact, in the keyboard component of the second example experiment, there’s a registerOn section that I don’t normally have on my 2022.2.2 version. When hovering the red cross it says “This parameter has come from an older version of PsychoPy. In the lastest version of PsychoPy, it is not used. Click this button to delete it. WARNING: This may affect how this experiment works in older versions!”
I don’t know much about how versions work but this is the only difference I see.
image

I tried running the experiment on version 2022.2.2, and it still works. Very odd…
You can try to update your PsychoPy version/try to run the experiment on another computer with a newer version of PsychoPy.
Also, you can try to delete the regsiterOn button, but I doubt it has anything to do with it…

I guess sharing your experiment wouldn’t help because my examples don’t work on your machine as well.

4 hours (and freezing my laptop twice) later, I’m happy to say it works! I uploaded the most recent version (v2022.2.5) and the experiment finally works! So strange that it didn’t work on the v2022.1.1.
Thank you for all your help and time :hugs: :pray:

P.S For future reference (to whoever may need it), it only worked when the code component was right under the slider component. When I added the code component to the bottom of the line within the routine (as it’s automatically added there) I couldn’t either skip or escape the routine at all. When I moved it up to the second place (right under the slider) it worked.

1 Like