psychopy.org | Reference | Downloads | Github

"Esc" to quit experiment and Feedback if no key is pressed inn 1700 msec

Good Morning,

I would like to allow the participant to quit the experiment any time when the “Esc” key is pressed and also to give the feedback as “incorrect” answer, and move to the next trial if the participant takes more than 1700 msec to answer a trial. Please, which code should I insert inside the loop?

Thanks in advance :slight_smile:

# Loop TRAINING
for trial_, trial in enumerate(trials):
    if trial_ == 24:
        break
 
# Present fixmark
imgfix.draw()
fixonset = disp.flip ()
wait (FIXTIME)

# Draw the fixation mark and the cues
imgfix.draw()
cuestim[trial["cueside"]].draw()
cueonset = disp.flip()
wait (CUETIME)

# Draw the fixation mark
imgfix.draw()
# Update the monitor 
cueoffset = disp.flip()
#wait for the SOA minus the cue duration
wait (trial["soa"] - CUETIME) 

# Draw the fixation mark
imgfix.draw()
# Draw a target stimullus
tarstim[trial ["tarside"]].draw()
# Update the monitor
taronset = disp.flip()

# Wait for a response
resplist = waitKeys (maxWait=float("inf"), keyList = ["left", "right"],timeStamped = True)

# Select the first response from the response list
response, presstime = resplist[0]

# Calculate de RT
RT = presstime - taronset

# Check if the response was correct
if response == tarstim [trial ["tarside"]].correctAns:
    correct = 1
else:
    correct = 0

# Valid cue?
if [trial ["cueside"]] == [trial ["tarside"]]:
    validity = 1
else:
    validity = 0

# Show feedback
fbstim[correct].draw()
disp.flip()
wait (FEEDBACKTIME)

Are you serious about 1.7 ms? I’m guessing it is a typo…

Indeed, sorry. I want the target to be presented until the participant responded, but for no longer than 1700 msec.

OK, so looking at your code, you’re going to need to edit it so we can see the correct indentation. We need to know what code is inside the trial loop.

Is this in Coder or Builder? I’m not seeing a loop so I’m wondering if you mean a Builder loop. However, you have wait and flip which shouldn’t be used in Builder.

# Loop TRAINING
for trial_, trial in enumerate(trials):
    if trial_ == 24:
        break

    # Present fixmark
    imgfix.draw()
    fixonset = disp.flip ()
    wait (FIXTIME)

    # Draw the fixation mark and the cues
    imgfix.draw()
    cuestim[trial["cueside"]].draw()
    cueonset = disp.flip()
    wait (CUETIME)

    # Draw the fixation mark
    imgfix.draw()
    # Update the monitor 
    cueoffset = disp.flip()
    #wait for the SOA minus the cue duration
    wait (trial["soa"] - CUETIME) 

    # Draw the fixation mark
    imgfix.draw()
    # Draw a target stimullus
    tarstim[trial ["tarside"]].draw()
    # Update the monitor
     taronset = disp.flip()

    # Wait for a response
    resplist = waitKeys (maxWait=float("inf"), keyList = ["left", "right"],timeStamped =    True)

    # Select the first response from the response list
    response, presstime = resplist[0]

    # Calculate de RT
    RT = presstime - taronset

    # Check if the response was correct
    if response == tarstim [trial ["tarside"]].correctAns:
        correct = 1
    else:
        correct = 0

    # Valid cue?
    if [trial ["cueside"]] == [trial ["tarside"]]:
        validity = 1
    else:
        validity = 0

    # Show feedback
    fbstim[correct].draw()
    disp.flip()
    wait (FEEDBACKTIME)
    ...

Hi Michael, I posted the correct indentation below.

thank you

Hi wakecarter, It’s in Coder. I posted the correct indentation below. thank you

In the call to waitKeys(), you can simply specify maxWait = 1.7

You should also add 'escape' to the list of allowed keys. If that was the key that was pressed, then call core.quit(). Test for that before you check if the response was correct (otherwise it would register as an incorrect response).

But you really should be using a TrialHandler object rather than a simple loop loop over a list of conditions, because that way your data should be saved automatically when quit() is called.

Thanks Michael,

I’m getting this error when no key is pressed:

response, presstime = resplist[0]
TypeError: ‘NoneType’ object is not subscriptable

Experiment ended.

I tried to use the TrialHandler but it was working only in the first loop, in the second loop is was like an infinite loop, I don’t know why.

If no response is made, the list is empty, so it doesn’t have an element 0. So first you need to check if the list is not empty. Only then can you extract an entry from it:

if resplist:
    # Select the first response from the response list
    response, presstime = resplist[0]
    
    # the rest of your code

else:
    # code for whatever happens if no response was made

thanks Michael