Block Feedback - problem with Python code

Hello,

I am trying to implement a block feedback to show the participant their accuracy and mean RT.

I used the instructions given here: https://www.psychopy.org/recipes/builderFeedback.html

All runs well locally. However, when I try to run it online, it does not run. I believe this is because when I write the code below while having the Auto>JS selected in the code window, the conversion finds issues.

The code is (Practice is the name of the block that will provide the data for the feedback, it has 32 trials):

nCorr = Practice.data[‘response.corr’].sum()
pernCorr = (nCorr100)/32
meanRt = Practice.data[‘response.rt’].mean()
meanRt_1000 = meanRt
1000
if pernCorr >= 80:
msg = ‘Good job!\n\nIn this block, you got %i percent of trials correct.\nYour reaction time= %i ms.\n\nPress space to continue.’ %(pernCorr,meanRt)
else:
msg = ‘Pay attention!\n\nIn this block, you got %i percent of trials correct.\nRemember to get at least 80 percent of trials correct.\nYour reaction time= %i ms.\n\nPress space to continue.’ %(pernCorr,meanRt)

Then I get this: /* Syntax Error: Fix Python code */

If I delete this specific part “%(pernCorr,meanRt)” the conversion runs well, but of course I do not get the feedback values, only the message text. How can I solve this? I cannot find what the problem is because apparently adding “%(pernCorr,meanRt)” is correct in order to generate formatted strings…

I really appreciate the help.
Best
Clara

Adding the ``` so that the code looks good :slight_smile:

nCorr = Practice.data['response.corr'].sum()
pernCorr = (nCorr*100)/32
meanRT = Practice.data['response.rt'].mean()
meanRT_1000 = meanRT*1000
if pernCorr >= 80:
    msg = "Good job!\n\nIn this block, you got %i percent of trials correct.\nYour reaction time=%i ms.\n\nPress space to continue." %(pernCorr,meanRT_1000)
else:
    msg = "Pay attention!\n\nIn this block, you got %i percent of trials correct.\nRemember to get at least 80 percent of trials correct.\nYour reaction time=%i ms.\n\nPress space to continue." %(pernCorr,meanRT_1000)

@clarappc, the error is related to your string formatting. To fix, you will have to change how you format your strings in Python using simple string concatenation e.g.,

msg = "Good, You got " + str(pernCorr) + "correct" # etc

However, you could also manually edit the JS code and use the following string template. Set the code type to “Both” and use:

msg = `Good, You got ${pernCorr} correct`  // note, use of backticks rather than quotes

@dvbridges, thank you very much for your reply!

I have tested the first approach (with Python string concatenation). In the code window (Auto>JS), now I see no more errors and all works when I run the experiment locally.

When I run it online, I get an error (see below); it is as if it does not find the “response.corr” in the Practice block… I have no idea why this may be happening. I am also adding the code in builder below:

In the Begin Experiment:

msg = 'doh!'

In the Begin Routine: “Practice” is the name of the block (routine with trials), and “response” is how I named the response key.

nCorr = Practice.data['response.corr'].sum()
pernCorr = (nCorr*100)/32
meanRt = Practice.data['response.rt'].mean()
meanRt_1000 = meanRt*1000
if pernCorr >= 80:
    msg = "Good, you got" + str(pernCorr) + "correct. Your reaction time in this block was" + str(meanRt_1000) + "Press space to continue."
else:
    msg = "Pay attention! In this block, you got" + str(pernCorr) + "Remember to get at least 80% of trials correct. Your reaction time in this block was" + str(meanRt_1000) + "Press space to continue."

The error I am getting when running the experiment online is on the screen below.

I am very grateful for your help!
Best
Clara