# Problem appending values and calculating scores with custom code (no error message)

OS: macOS
PsychoPy version (e.g. 1.84.x):
Standard Standalone? (y/n) Y

For my experiment I need to include a variable that is the sum of the number of correct answers in the last ten trials to calculate the points for each trial.

In order to do this I created a trial number variable using custom code in the trial routine (begin experiment: trialN = 0. End routine: trialN = trialN + 1). I displayed the trial number to test if it is working and it is working fine. However, I can’t get my trial score and total scores in the feedback routine to work.

In the feedback routine I am attempting to:

1. calculate h = the sum of the number of correct choices in the last 10 trials
(for the first round h = 5 since there are no previous choices, but after that h goes back to being sum of number of correct choices in the last 10 trials)
2. calculate the trial score for choosing the correct answer for each trial 30 + 5*h
3. calculate the trial score for choosing the incorrect answer for each trial 40 + 5*h
4. store the trial score and the totalscore (totalscore = totalscore + trialscore)

To check if my code was working, used text to display the totalscore variable, however it only flashes 0 (the number I initialized the totalscore to at the beginning of the experiment).

I am not getting any error messages so I am not sure if there is a problem with my code, or if I put the codes in the wrong part of the custom code.

Feedback routine:

BEGIN EXPERIMENT:

``````gainstotal = 0
gainstrial = 0
lastTen = []    #initialize array to append to
firstH = 5
h = 0
``````

BEGIN ROUTINE:

``````import numpy as np

#for first trial they get 5 pts, after that it will be the number of correct answers in the last ten trials

if trialN == 1:           #for first trial, h = 5
if key_resp2.corr == 1:
lastTen.append(firstH + 1)
elif key_resp2.corr == 0:
lastTen.append(firstH + 0)

#keeping track of correct or incorrect trials
else:
if key_resp2.corr == 1:
lastTen.append(1)
elif key_resp2.corr == 0:
lastTen.append(0)

#calculate h (sum of last 10)
if trialN < 11:
h = np.sum(lastTen[ : ])      #if there are not 10 values in the array yet, add all the values
else:
h = np.sum(lastTen[-10: ])    #if there are more than 10, add the last 10 values

``````

EACH FRAME:

``````if key_resp2.corr == 1:
gainstrial = 30 + 5*h
else:
gainstrial = 40 + 5*h
``````

END ROUTINE:

`````` gainstotal = gainstotal + gainstrial
``````

If anyone can help me I would really appreciate it!

Thank you!

Hello Joy,

Sorry I think I lost track of some previous posts but it looks like you are making good progress.

Just a number of small suggestions, only some of which might be related to your issues:

This import isn’t necessary, as Builder does it by default (but doesn’t do any harm in this case).

You mention that code in the text of your post, but don’t actually include it with the rest so it can be seen in context. Regardless, PsychoPy maintains various trial counters for you. For example, if your loop is called `trials`, then you can access `trials.thisN`, so keeping count yourself isn’t necessary.

Remember, the first trial is actually the zeroth, so should be `== 0`. This might cause some issues for you.

This code really belongs in the “End routine” tab along with the total calculation, as it only needs to be executed once, rather than repeatedly, many times per second.

Remember, you’re counting from zero, so should be `< 10`

Hi Michael,

Thank you so much for your help. I used trials.thisN instead of my own counter (thank you for that info!).

I also changed if trial.thisN == 1 to == 0 and changed trials.thisN <11 to trials.thisN <10.

I also moved the each frame code to the end routine tab.

I tried running it again and it is still displaying 0 for the total score and the output is not giving me any errors.

I am attaching my file and the conditions excel file, hopefully that will be more helpful than my description. I am wondering if I did something wrong in my conditions file, or if the problem is in my code.

testcond2.psyexp (11.3 KB)
Conditions.xlsx (34.2 KB)

Hi @jpw525,

If you are having problems updating the score at the bottom of the screen in your experiment, try changing the text_2 component to update on every repeat by selecting “set every repeat” from the component drop down list next to “Text” box, rather than using “constant”.

2 Likes

thank you David! What a silly mistake, that fixed it.

One other issue I am having is that I am trying to add the last 10 numbers in an array but it is adding the entire array.

#calculate h (sum of last 10)

``````if trials.thisN < 10:
````h = np.sum(lastTen[ : ]) #add the entire array if less than 10 in the array
else:
````h = np.sum(lastTen[-10: ])  #this is not working
``````

In cases like this, put in some `print()` statements to check on what is happening with your logic:

``````if len(lastTen) < 10: # don't actually need to count trials
h = sum(lastTen) # can use the regular Python sum function
print('<10')
else:
h = sum(lastTen[-10:])
print(h)
``````

But actually there seems to be a quirk in Python indexing where it doesn’t matter if the negative index exceeds the length of the list (i.e. you won’t get an out of bounds error), so you could just do this:

``````h = sum(lastTen[-10:])
``````

Regardless of the length of the list.

I think that worked, thank you!

Michael

``````    January 22
``````

In cases like this, put in some `print()` statements to check on what is happening with your logic:

``````>     if len(lastTen) < 10: # don't actually need to count trials
> h = sum(lastTen) # can use the regular Python sum function
> print('<10')
> else:
> h = sum(lastTen[-10:])
> print(h)
``````

But actually there seems to be a quirk in Python indexing where it doesn’t matter if the negative index exceeds the length of the list (i.e. you won’t get an out of bounds error), so you could just do this:

``````> h = sum(lastTen[-10:])
``````