# Calculate the percentage of correct answers in each condition

Hi everyone, I am trying to add a code component that allow me to calculate the percentage of right answers for each type of condition. I specify that the conditions for this task are in an excel file. So I am asking how would you write the if to have the percentage of right answers calculated for a given condition?

Hello,

do you define the correct answer in your Excel-file? If so, PsychoPy write a column named key_resp.corr (1 = correct, 0 = incorrect; use the name you provided). You could simply add key_resp.corr to a variable and calculated the percentage of correct answers. Given that you want the percentage correct by condition, you need as many variables as you have conditions.

Best wishes Jens

Hi and thanks for replying, I want to ask you how I can assign a variable to a certain condition that is on excel. I tried this way: I took the variable that is filled with the image (which is called “stimulus”) to which the subject responds and wrote, for example, if stimulus==“images/1.jpg”, but I saw that it doesn’t enter the if.

Hello MTH

well, instead of checking for a particular stimulus, I had a column in the Excel-file which specifies the condition and a column which specifies the correct answer. Then I’d test the condition of the current trial and test whether the answer was correct or not.

For example, after the response, (End routine Tab of the relevant routine or Begin routine tab of the following routine) has been given

``````if key_resp.corr:
if cond == "even":
corrEven += 1
else:
corrOdd += 1
``````

In the example, there are two conditions, cond, which can be even or odd. Given that I defined correct answers depending on the condition, right for even and left for odd, I simply count the number of correctly given responses.

I initialize the variable, corrEven and corrOdd in a Begin experiment tab with 0.

Best wishes Jens