Showing Correct Response on Next Routine

The short version of the issue: I have an experiment that shows 4 random numbers that need to be added together (i.e., on each trial, you would see something like 8+3+5+1). Participants type in their response, then should be shown their response and what the correct response was. The numbers, the participant response, and the correct response are all recorded in the data output. I cannot get the experiment to show the correct response on the next routine, nor does it write the correct response to the data in the order that it appears on the screen.

What I have right now:

On the routine that shows the equation:

Begin Experiment

#generate four random numbers to be inserted into the text - this just generates something for them to exist - these could probably be assigned 0s or something, they're just placeholders 
numOne = randint(1,10)
numTwo = randint(1,10)
numThree = randint(1,10)
numFour = randint(1,10)

Begin Routine

#generate the numbers for the actual routine 
numOne = randint(1,10)
numTwo = randint(1,10)
numThree = randint(1,10)
numFour = randint(1,10)

Then, on the next routine where they get feedback:
Begin Experiment

corrResp = 0

Begin Routine

corrResp = numOne + numTwo + numThree + numFour
thisExp.addData("corrResp",corrResp)

corrResp is put in my text element, like so:

Starting on the second trial, it shows corrResp as the actual correct number, but on the first trial it shows corrResp as 0. I imagine this is because it isn’t correctly writing corrResp yet, but I can’t figure out why. If I move the code of “corrResp = numOne + numTwo + numThree + numFour” to the End Routine section of the first routine, then it actually shows me the correct response for the next loop iteration, essentially giving the participant the correct response for the next trial.

The second issue is that it does not seem to be writing the corrResp into my data in the correct order. The initial assignment of 0 doesn’t appear in my data, even though it shows that on screen, and then it writes some of the correct corrResp to seemingly random rows and other times I can’t figure out where the corrResp number in the data came from.

The best place to create corrResp would probably be straight after the code where you generate the numbers themselves (Begin Routine in your trial).

Your Begin Experiment code should probably set the numbers to 0

Your code components should be at the tops of your routines

Thanks Wake - it works now! Not sure if it was the begin experiment code or moving the code components to the top - since at one point I did put the corrResp in the Begin Routine section - but either way, it’s working!

Here is the new code, in case anyone comes across this in the future and needs the specific output:

Begin Experiment

numOne = 0
numTwo = 0
numThree = 0
numFour = 0

Begin Routine

numOne = randint(1,10)
numTwo = randint(1,10)
numThree = randint(1,10)
numFour = randint(1,10)

corrResp = numOne + numTwo + numThree + numFour