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.

Hello,
When i am running this code in my End Routine

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

following error message is appearing every time

In the Begin Experiment, i have the following code

accuracy = 0
candies = 0

And my image component looks like this

Please help me identify the mistake here. Am I missing anything?

Also, how should I modify the code if I have to show different images depending on the amount of candies one has received, as I have mentioned in my previous discourse (say image Candy1.jpg has one candy; image Candy2.jpg has two candies; image Candy3.jpg has three candies and so on…). I want to show the particular image depending on the number of candies one has received (max here could be 8 candies; since there are 80 main trials, assuming the child gets one candy every 10th trial).

Thanks for helping me so far.

You need to have some kind of default in Begin Experiment, e.g.

candieImage = "Candy0.jpg"

On the 4th trial, it is now showing the following error message

I added another default image in the Begin Experiment

accuracy = 0
candies = 0
candieImage = "reward/EmptyBox.png"

But in the error now, somehow its taking extra ‘0’ in the image.

Try print('RW_PPC.thisN',RW_PPC.thisN,', candies',candies) at the top of End Routine to track what’s happening.

The code didn’t go beyond trial trial 3

and again showed the following error

Please could you show your current code for candieImage = "reward/Candy1" + str(candies) + ".jpg" ? The 0 has just moved

The code looks like this

continueRoutine = True
print('RW_PPC.thisN',RW_PPC.thisN,', candies',candies, 'accuracy', accuracy)
if correct and RW_PPC.thisN > 2:
    accuracy += 1

if (RW_PPC.thisN - 3) % 10 == 9:
    if accuracy > 6:
        candies += 1
    if candies > 0:
        continueRoutine= False
    accuracy = 0
    candieImage = "reward/Candy1.png" + str(candies)

Even if I write the code like

continueRoutine = True
print('RW_PPC.thisN',RW_PPC.thisN,', candies',candies, 'accuracy', accuracy)
if correct and RW_PPC.thisN > 2:
    accuracy += 1

if (RW_PPC.thisN - 3) % 10 == 9:
    if accuracy > 6:
        candies += 1
    if candies > 0:
        continueRoutine= False
    accuracy = 0
    candieImage = "reward/Candy1" + str(candies) + ".png"

The only difference it makes is that the 0 shifts its place. The code is not running beyond trial number 3.

The code should be candieImage = "reward/Candy" + str(candies) + ".png"

You hard coded a 1 in front of the variable

exp2_stims.xlsx (31.4 KB)
Candy1
Candy2
Candy3
Candy4
Here “Candy1” is the name of the image which is located in the folder named “reward”.

I am attaching the experiment, excel file and sample images to make things more clear.
exp2-RW - Copy.psyexp (533.3 KB)

If the value of candies = 0; the code should call the image Candy1.png; if it is candies=1, the code should call the image Candy2.png; and so on.

Should I change the name of the image?

Try candieImage = "reward/Candy" + str(candies+1) + ".png"

Now the error on the 4th trial looks like this

I presume the problem lies somewhere when the code is counting the number of candies.

Please show your code again.