psychopy.org | Reference | Downloads | Github

Same logging code for two loops

EmoMusStudy_caspartest (1).psyexp (33.5 KB)

Mac 10.14.5
**PsychoPy 3.1.0

Hi all,

I use the builder to create a code (under custom view) and have two loops. Both loops are the same, except the second one has just music background.

from psychopy.hardware import keyboard
from psychopy import core
from psychopy import logging

kb = keyboard.Keyboard()

during your trial

kb.clock.reset() # when you want to start the timer from
kb.getKeys(clear = False) #clear keyboard buffer

routineLength = kb.clock.getTime()
logging.data(msg='routine time: '+ str(routineLength))
keys = kb.getKeys([‘right’, ‘left’], waitRelease=False)

l= 0
r = 0 #keep track of total presses.

#loop through array of key presses and collect length of left and right.
for key in keys:
logging.data(msg=‘key:’ + key.name + ‘, RT:’ + str(key.rt) + ‘, dur:’ + str(key.duration))
logging.log(level=logging.WARN, msg=‘ourkeypress:’ + key.name + ‘, RT:’ + str(key.rt) + ‘, dur:’ + str(key.duration))
if key.duration is None:
#key was held down to end of routine so duration is length - start
dur = routineLength - key.rt
else:
dur = key.duration
if key.name == ‘right’:
r = r + dur
elif key.name == ‘left’:
l = l + dur
continueRoutine = True

logging.data(msg='Left Total: ’ + str(l))
logging.data(msg=‘Right Total:’ + str®)

#write the totals into the CSV file…
thisExp.addData(‘Left.Total’, l)
thisExp.addData(‘Right.Total’, r)

My code is working for the first loop but it does not work for the second one. How can I merge them?

Could you help me what I am missing here?

Thank you.
Best,
Zehra