psychopy.org | Reference | Downloads | Github

Clock/Timer Troubles

Hi all,

I promise one day I’ll stop spamming these forums :sleepy:

So I’m having real trouble with setting up a clock to end a routine. I’ve tried lines of code grabbed from every source I think of but nothing seems to work and I don’t really understand how the clocks themselves work so I’m having trouble identifying the issue.

All I need is for the start of the routine (which is being looped) to begin a timer of 2 seconds. Once the 2 seconds is up the routine needs to stop and execute the following:

failed_attempts += 1
        if failed_attempts >= 3:
            score -= 1
            action = 3
            message = 'Unsuccessful. \n -1 Point'
            trial_loop.finished = True

How do I do this?

Any advice greatly appreciated!

CK

Are you using Builder or writing your own experiment from scratch?

Hi,

Using builder to present some text components, images etc. but using code components to make it track scores and stuff. I can post the whole code/screenshots later if it’s helpful.

In that case, just set your routine to have a fixed duration of 2 seconds (i.e. set all stimulus components to end at or before t = 2.0 seconds).

Then insert what code you want to run in the “end routine” tab of a code component. It will automatically run for you at the right time.

But you also probably need to explain to us what your code is supposed to achieve. e.g. At the moment it seems to unconditionally treat all attempts as failed. Is that actually what you want?

Hi Michael,

I think I’ve made a little progress but still getting a bit stuck. For context, I’m designing a task that presents ‘aggressors’ to a participant and they have to respond with ‘lethal’ or ‘non-lethal’ force to accumulate/protect their points. Using non-lethal force is only successful 1 in 5 times but awards points upon success. Lethal is successful 100% of the time but awards no points.

To ensure their responses are prompt, I want to impost a time limit on each presentation - failing to respond within that time limit should end the presentation and mark a penalty against them failed_attempts += 1 Accumulating 3 failed attempts deducts a point and ends their interaction with that particular ‘aggressor’.

if failed_attempts >= 3:
           score -= 1
           action = 3
           message = 'Unsuccessful. \n -1 Point'
           trial_loop.finished = True

At the moment I have two routines within a loop. The first routine executes the code for checking if their action was successful:

if 'q' in response.keys and response.corr == False:
    failed_attempts += 1
    if failed_attempts >= 3:
        score -= 1
        action = 3
        message = 'You were not able to end the conflict. \n -1 Point'
        trial_loop.finished = True

if 'q' in response.keys and response.corr == True:
    score += 1
    action = 1
    message = 'You successfully ended the conflict with non-lethal force. \n +1 Point'
    trial_loop.finished = True

if 'p' in response.keys:
    action = 2
    message = 'You successfully ended the conflict with lethal force.'
    trial_loop.finished = True

The second routine checks if there’s anything in response.keys and if so, ends the routine. If it’s empty, it should mark up a failed attempt as described:

if response.keys:
    continueRoutine = False
else:
    failed_attempts += 1
    if failed_attempts >= 3:
        score -= 1
        action = 3
        message = 'You were not able to end the conflict. \n -1 Point'
        trial_loop.finished = True

As suggested, I’ve set the components to all end after 2 seconds but now I get the following error:

if ‘q’ in response.keys and response.corr == False:
TypeError: argument of type ‘NoneType’ is not iterable

I hope that has made some sense - still learning a lot!

So it looks like response.keys is None, so its contents can’t be checked. If so, you should put this before your code:

if response.keys is not None:

and indent all of the other code accordingly.

This is just a more explicit check of what you do later with if response.keys: but you do have to guard against this condition (no response having been made) at the first stage as well.

Michael thanks a bunch for your help - this has worked perfectly!

No I doubt I’ll be back with some other problem soon enough!

Cheers
CK