psychology - psychopy builder experiment with feedback and multiple if loops -
i trying use coder view make experiment involves feedback , multiple conditional statements. how do though? task involve 4 math problems, , participants allowed have 3 attempts each problem. structure should this...
loop 1: goes through 4 problems
loop 2: allows 3 attempts per problem
loop 3: if response correct, "correct" , move onto next problem; else, "incorrect" , ask if try again or move on
this first time using python , can't work out kinks in code. there no error message returned, rather, code not register response , task frozen in place @ prompt screen. code below. didn't include libraries , other setup.
t=0 nproblem=4 nattempt=3 while currentprob <= nproblem: problemtimer.reset() attempt = 1 # *prompt* updates prompt.setautodraw(true) prompt.settext(u'problem prompt go here.\n\ntype in answer , press enter submit.', log=false) while attempt <= nattempt: response = [] # *response* updates core.wait(1) #lb - using in place of enormous if-statement event.clearevents(eventtype='keyboard') thesekeys = event.getkeys(keylist=['1', '2', '3', '4', '5', '6', '7', '8', '9', '0']) # check quit: if "escape" in thesekeys: endexpnow = true if ('1', '2', '3', '4', '5', '6', '7', '8', '9', '0') in thesekeys: # subject responds number value response.append(thesekeys) # tack on responses previous responses while event.getkeys(keylist=['return'])==[]: # *timer* updates if t <= 0.0: # keep track of start time/frame later timer.setautodraw(true) #elif timer.status == started , t >= (0.0 + (600-win.monitorframeperiod*0.75)): #most of 1 frame period left #timer.setautodraw(false) #if timer.status == started: # update if being drawn timer.settext(str(round((600+routinetimer.gettime())/60,1)) , log=false) # *minutesleft* updates if t >= 0.0: # keep track of start time/frame later minutesleft.setautodraw(true) #elif minutesleft.status == started , t >= (0.0 + (600-win.monitorframeperiod*0.75)): #most of 1 frame period left #minutesleft.setautodraw(false) numberlist=event.getkeys(keylist=['1','2','3','4','5','6','7','8','9','0','backspace','return']) number in numberlist: #if key isn't backspace, add key pressed string if number !='backspace': response.append(number) #otherwise, take last letter off string elif len(text)>=0: response.remove(numberlist[number-1]) #continually redraw text onscreen until return pressed answer = visual.textstim(win, text=response,color="black",pos=(0,-100)) answer.draw() win.flip() event.clearevents() if len(thesekeys) > 0: # @ least 1 key pressed response.keys.extend(thesekeys) # storing keys response.rt.append(response.clock.gettime()) #check quit if "escape" in thesekeys: endexpnow = true if response == '9999': # correct? correctans = 1 else: correctans = 0 if thesekeys == 'enter': response.keys.extend(thesekeys) # storing keys response.rt.append(attemptresponse.clock.gettime()) if correctans == 1: attempt += 888 #ends , goes next problem currentprob += 1 datafile.write(attempt,attemptresponse,thesekeys,response,correctans) #output separated commas #datafile.write('pid cond prob att time resp\n') response_correct.draw() win.flip() event.waitkeys() if correctans == 0: attempt += 1 #lb = setting 1 forever datafile.write(attempt-1,attemptresponse,thesekeys,response,correctans) #output separated commas response_incorrect.draw() win.flip() thesekeys = event.getkeys(keylist=['q','space']) if thesekeys == 'q': continueroutine = false if thesekeys == 'space': prompt.draw() win.flip() event.waitkeys()
i cannot write complete code point out few things of way.
getting responses
thesekeys = event.getkeys(keylist=['1', '2', '3', '4', '5', '6', '7', '8', '9', '0']) if "escape" in thesekeys:
here thesekeys
can contain stuff in keylist
"escape" never there. extend keylist "escape" - , possibly "enter" use later.
if ('1', '2', '3', '4', '5', '6', '7', '8', '9', '0') in thesekeys:
thesekeys
list , when "(x,y,z) in list", element (x,y,z) , not occurrances of x, y , z. if have keylist above, know non-escape response 1 of these it's unneccessary. do
thesekeys = event.getkeys(keylist=['1', '2', '3', '4', '5', '6', '7', '8', '9', '0', 'escape']) if "escape" in thesekeys: core.quit() # assuming psychopy.core imported elif "enter" in thesekeys: # something else: response.append(thesekeys[0]) # obs: pick first response
loops
it looks want use for
loop rather while
loop. using for-loops, don't have keep track of current loop number. rather than
while currentprob <= nproblem:
do
for currentprob in range(nproblem):
then increment of currentprob
happens automatically , loop terminated when should. more elegant when possible.
waiting responses
i'm bit in doubt whether want use event.waitkeys()
rather than event.getkeys()
in while-loop, , use maxwait
argument if want control waiting time , use core.clock()
if want track timing. while loop ok if want animate if not, event.waitkeys()
simpler , safer.
and lastly, while try make work, use lot of print
statements check actual contents of thesekeys
, other variables. it's best way debug , capture gotchas.
Comments
Post a Comment