psychopy.org | Reference | Downloads | Github

Failure to append variables

code

#1

What are you trying to achieve?:
Hi!
I need to append three variables and keep count of the scores. Once a certain score is reached I will terminate the loop.

What did you try to make it work?:

The task I am creating is a probabilistic learning task.
The scores should be counted during the learning phase of the task, however there are three different conditions and each condition has a separate criterion that needs to be reached before the loop can be terminated.

Therefore, I have created 3 new variables (resembling the resp.corr variable) but calculated specifically for the given condition. 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 as I added variables resp1, resp2 and resp3 to the excel output. 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 variables in the excel output to check if they are calculated properly.

What specifically went wrong when you tried that?:

Unfortunately, replist1, replist2 and replist3 fail to append the list. Instead the values of resp1, resp2 and resp3 are printed in []. Picture below:

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 means were exactly the same values as resp1, resp2 and resp3.

I am not sure why the list is not being appended correctly and I will appreciate all help!


#2

Hi @MartaT, at which point in the experiment are you defining your replists? If you define them at the beginning of each trial then you will get a fresh empty list on every trial.


#3

Hi @dvbridges,
You are right, the resplists were defined in the begin routine section.
I overlooked the fact that this could be the reason - can’t believe it was this simple!

Thank you so much!
All works very well now!