Reputation: 43
I am creating a probabilistic learning task. It has a learning phase which is what I am preparing at the moment using the builder interface with custom code in PsychoPy. The learning phase has at least 60 trials in a loop called practice
.
Apart from the correct answer which is used to give feedback to participants, there are three conditions by which it can be decided whether the learning phase can be finished. Once the criteria are reached, the training phase will be terminated.
I need to append three variables and keep count of the scores.
I have created 3 variables (resembling the resp.corr variable) calculated specifically for the three conditions. The code is placed in the ‘end routine’ section because in the earlier sections resp
was not yet defined.
End routine
if (resp.keys == letterA):
resp1 = 1
else:
resp1 = 0
if (resp.keys == letterC):
resp2 = 1
else:
resp2 = 0
if (resp.keys == letterE):
resp3 = 1
else:
resp3 = 0
This is working fine. I added variables resp1, resp2 and resp3 to the excel output. I checked and they are all calculated correctly.
So i know that I need to append these variables in a list and I used the following code:
End Routine
resplist1.append(resp1)
resplist2.append(resp2)
resplist3.append(resp3)
I also saved these lists in the excel output to check if they are calculated correctly. I used the following code:
End Routine
practice.addData('resplist1', resplist1)
practice.addData('resplist2', resplist2)
practice.addData('resplist3', resplist3)
Unfortunately, replist1, replist2 and replist3 fail to append the list. Instead the values of resp1, resp2 and resp3 are printed in []. Picture at the bottom:
imageimage.png1635×655 34.1 KB
I also checked whether it was possible to calculate the sum of replist1, replist2 and replist3 and as you can guess it did not work. The calculated values were exactly the same as resp1, resp2 and resp3.
I am not sure why the list is not being appended correctly and I will appreciate all help! I have been stuck on this task for way too long now and I am desperate to have it finished.
https://i.sstatic.net/aXW60.png
Upvotes: 4
Views: 179
Reputation: 2421
You don't show how you initialise your lists resplist1
etc. My guess is that you are doing so in the "Begin routine" tab. That would continually reset them so that they never contain more than one value.
If so, shift their initialisation to the "Begin experiment" tab so you don't lose the newly appended values on every trial.
When you have a list that actually contains multiple elements, the easiest way to do calculations upon it is to convert it to a numpy
(imported as np
) array, which allows vectorised operations, e.g:
sum_1 = np.array(resplist1).sum()
Upvotes: 1