Showing reward after every 10 trials based on the mean accuracy

OS (Win11):
PsychoPy version (2024.1.3):
What are you trying to achieve?:
I have cued switching task. My goal for the experiment is to show a reward screen after every 10 trials. The reward will be based on the mean accuracy of the last 10 trials, which should be above 60%. In other words, if the mean accuracy of the last 10 trials is above 60%, after the 10th trial we show them a reward screen. This should happen throughout the experiment after every 10 trials (e.g., 8 times if there are 80 trials). For the first time the reward should appear after 13th trial, since the first 3 trials will not be included in the accuracy calculation. For the remaining 70 trials, the reward should appear after every 10th trial only.

As I am new to coding, I am not sure how to achieve this in psychopy builder. It would be helpful if I can get help with this.

Best
Tripti

How about the following code component?
This assumes that your accuracy is based on a keyboard component called key_resp and your loop is called trials

Begin Experiment

accuracy = 0

End Routine

skipReward = True

if key_resp.corr and trials.thisN > 2: 
     accuracy += 1

if (trials.thisN - 3) % 10 == 9:
     if accuracy > 6:
          skipReward = False
     accuracy = 0

Then go to the routine settings for your reward routine and put skipReward into the skip if field.

Hey
Sorry for such a late response. I tried the code, but it is showing syntax error for one of the ifs.

if
^
SyntaxError: invalid syntax

Also, I would like to request one more thing into the problem. The reward screen will have different images with captions depending on the amount of reward the participant has collected. Say for example, if the participant gets reward after every 10th trial (i.e. they are meeting the criterial of scoring more than 60% by every 10th trial), they will be shown images of 1, 2 or more candies respectively.
For instance:
At the end of the 10th trial, mean accuracy is 70%, the participant is shown an image of 1 candy.
At the end of 20th trial, he fails to score above 60%, he will be shown an image of only 1 candy here (no addition).
At the end of 30th trial, he sores 80%, he will be shown an image of 2 candies.
At the end of 40th trial, he sores 70%, he will be shown an image of 3 candies.
At the end of 50th trial, he sores 40%, he will be shown an image of 3 candies.
At the end of 60th trial, he sores 90%, he will be shown an image of 4 candies.
And so on.

I hope I am clear in making my problem stated here. Please let me know if this is still confusing.

Please could you show your End Routine code?

skipReward = True

if key_resp.corr and trials.thisN > 2: 
     accuracy += 1

if (trials.thisN - 3) % 10 == 9:
     if accuracy > 6:
          candies += 1
     if candies > 0:
          skipReward = False # show reward image if there have been any rewards
     accuracy = 0
     candieImage = "images/candies" + str(candies) + ".png" # show $candieImage

You’d also need to add candies = 0 to the Begin Experiment code

Following is my Begin Experiment code:

accuracy = 0
candies = 0

Following is End Routine code:

skipReward = True
 
if correct and RW_PPC.thisN > 2:
     accuracy += 1
 
 if (RW_PPC.thisN - 3) % 10 == 9:
     if accuracy > 6:
         skipReward = False
     accuracy = 0
     candieImage = "reward/candy1" + str(candies) + ".jpg"

where correct stores the accuracy (TRUE or FALSE) and RW_PPC is the name of the loop
reward is the name of the folder with stim image and candy1 is the image with one candy (there will be more images with different number of candies).

This might have been me when I was fixing your preformatted text tags but check your if statements are in line.

Thanks for fixing it. I forgot to put the backticks while editing the query.