| Reference | Downloads | Github

Psychopy point counter and conditional task


I am a beginner in psychopy and I am trying to build the following task.

One trial could be defined as:

There is a stimulus in the screen, if the participant press space all the responses will be registered.
He will earn points in a point counter (just the numbers on a text file) if he presses space after a specific amount of seconds has passed (e.g. 5 seconds).

I am trying to develop how to insert this using the builder, but it seems impossible without the code.

Could anyone please help me?

Thanks a lot!


IT is likely that you could do this using Builder but that you will simply need to insert a Code Component to give the small amount of extra behaviour you need. However, for anyone to give suggestions on how to do that would require a much more detailed and precise description of your procedure (as would be provided in a Methods section of a paper).

1 Like

Hi Michael, I am sorry, I tried to be more concise. I will try to explain. it better now.

During this task I will present on the center of screen in a alternated sequence one of two different symbols (A or B) each one for 10 seconds, and a point counter at the top of the screen (just a text showing a variable = 0). The participant will be instructed to press the space bar to produce points.

I see that naturally psychopy register (if I choose the correct options) all the space presses and all the time they happened - This is also an important data for my experiment.

The issue is that the point will be only added in the counter if the participant press the spacebar after a certain amount of time (e.g. 5 seconds). In other words, the stimulus A is on the screen, the participant start to press the space bar and every press before five seconds will produce no change, but the first press after 5 seconds will give him one point. If he does not press the space bar after 5 seconds, and the total time of the stimulus on the screen pass (e.g. 10 seconds) he does not make any point and the second symbol (B) is presented.

During B symbol he have to press the spacebar again, but he will make a point only if he presses it after second 3 and another one after second 6 for example. Again, if he presses earlier than 3 nothing happens. The first press after 3 start the counting to check if he will be able to gain another point after six only. If he presses only after 6 seconds he gains only one point. If he does not press after any of these times the B symbol will end after ten seconds.

Basically the procedure is a variable interval (but it does not have to be randomly assigned, I could insert the exact values of the interval symbol per symbol) and only if the participant presses the space bar after the determined interval a point will be added in his point counter.

I am familiarized with all the builder options in Psycho py. However to make this point counter works and also to include the condition that the participant have to press after a certain interval is what is making me struggle.

If you have any suggestion I would appreciate a lot!

Thank you very much!

Do you want to record the presses that occur before the start of, say, the 5 s period, and just not add them to the score, or do you want nothing at all to happen for key presses before 5 s?

Hi Michael,

I would like to record the frequency and also the response time of each of them.

I noticed that Psycho py is already registering these details under the space_resp.keys and space_resp.rt .

Thank you very much again.

OK. You could insert some code in the “end routine” tab of a code component that goes something like this:

score = 0
for rt in space_resp.rt:
    if rt >= 5.0:
        score = score + 1

# save in the data file:
thisExp.addData('score', score)

You could replace the hard-coded 5.0 with a variable name if required.

1 Like

Thank you very much Michael! I will try it and post if it was solved here! Thanks a lot for your replies! It was awesome!!!

Hi Michal, sorry to bother again,

I did the code exactly as you suggest however it is showing a invalid syntax. I will upload the images here so if you have any suggestion, just let me know.

I cannot thank you enough.

PS: I corrected the extra spaces in accidentally inserted in the last line of the code.

Hi, you are missing the colon characters at the end of both the for and if lines.

(and you were right about the unintended indenting, now corrected).

I corrected this silly mistake I made. It started to crash with the message that nam ‘score’ is not defined.

I looked in the forum and found out that the text_score shouldn’t be constant and set it to ‘set every repeat’.

Another error was happening showing this message: "score_text.setText(score). and it is apparently solved including a score column in the xlsx file. However just including the column presents at the top “default text”, if I put a number there (e.g. 0) it shows “0.0” however even if I press the space after five seconds it is not adding the points.

I actually do not need the xlsx file, because I can do in the builder all the changes in the presentation of the stimuli one by one. But I do not know if there is any other way to define the score_text as a number that will be added if the requirements of pressing the space bar after 5 seconds.

Thank you very much for all the attention.

You shouldn’t have a column in your conditions file for the score, as that conflicts with score actually being calculated (rather than specified in advance).

The arrangement in your screenshots doesn’t really work. The score is only calculated at the end of a trial, so it can’t be displayed throughout that trial, as the current value for this trial doesn’t exist yet.

You need to specify more clearly what you are trying to achieve. If you want real-time feedback, then the score calculation needs to be in the “every frame” tab, and only the saving the score to the data file should be in the “end routine” tab. Also, the code component should be above the text component, so that the text component always has access to the latest calculated value.

Im am sorry, for giving confuse information. Ideally the point counter would be always visible and every time the condition is reached the point would be added. it starts on zero but it will increase and keep the value for the rest of the procedure.

If I get your suggestion correctly (sorry I am a real noob) I should insert the first lines in every frame tab of the code and let only the last one in the end routine tab.

I didn’t understand the end “Also, the code component should be above the text component, so that the text component always has access to the latest calculated value.”

you mean in the builder I should put the code above the text?

And I did not get how I could do to make the score visible on the screen. I tried to insert different codes (like print from python) however nothing seemed to work. I imagine that the score is defined as a variable when you say (score=0), but the only thing that worked for showing was the xlsx ($score in the builder and the column in the file). I see that it is not correct but how should I proceed?

I am sorry to bother you.

Thank you very much again!


Yes. Right click on the icon to change its vertical ordering.

The calculated score is just a Python variable like any that you get from your conditions file. So simply put $score in the text field field of your text component, and set it to update on every frame.

That is why the ordering of components is important. We want the score to be freshly calculated before the text component refers to it.

Thank you very much!

I did all the changes, and this message is showing up.

It is the same before I tried the score column.

If you have any suggestion, just let me know please.

Thank you again!

As above:

The score variable doesn’t yet exist at the beginning of the first routine.