心理建造者用反馈和多个if循环进行实验



我正试图使用Coder视图来进行一个涉及反馈和多个条件语句的实验。你是怎么做到的?该任务最终将涉及4道数学题,参与者将被允许对每道题进行最多3次尝试。结构应该是这样的。。。

循环1:通过的4个问题

循环2:每个问题最多允许3次尝试

循环3:如果答案正确,那么说"正确",然后进入下一个问题;否则,说"不正确",并询问他们是否想再试一次或继续

这是我第一次使用Python,我无法解决代码中的问题。没有返回错误消息,但代码没有注册响应,因此任务在提示屏幕上被冻结。代码如下。我没有包括库和其他设置。

t=0
nProblem=4
nAttempt=3
while currentProb <= nProblem:
    problemTimer.reset()
    attempt = 1
    # *prompt* updates
    prompt.setAutoDraw(True)
    prompt.setText(u'Problem prompt will go here.nnType in your answer and press enter to submit.', log=False)
    while attempt <= nAttempt:
        response = []
        # *response* updates
        core.wait(1) #LB - using this in place of that enormous if-statement
        event.clearEvents(eventType='keyboard')
        theseKeys = event.getKeys(keyList=['1', '2', '3', '4', '5', '6', '7', '8', '9', '0'])
        # check for quit:
        if "escape" in theseKeys:
            endExpNow = True
        if ('1', '2', '3', '4', '5', '6', '7', '8', '9', '0') in theseKeys: # subject responds with number value
            response.append(theseKeys) # will tack on responses to previous responses
        while event.getKeys(keyList=['return'])==[]:
            # *timer* updates
            if t <= 0.0:
                # keep track of start time/frame for later
                timer.setAutoDraw(True)
            #elif timer.status == STARTED and t >= (0.0 + (600-win.monitorFramePeriod*0.75)): #most of one frame period left
                #timer.setAutoDraw(False)
            #if timer.status == STARTED:  # only 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 for later
                minutesleft.setAutoDraw(True)
            #elif minutesleft.status == STARTED and t >= (0.0 + (600-win.monitorFramePeriod*0.75)): #most of one frame period left
            #minutesleft.setAutoDraw(False)
            numberlist=event.getKeys(keyList=['1','2','3','4','5','6','7','8','9','0','backspace','return'])
            for number in numberlist:
                #if key isn't backspace, add key pressed to the string
                if number !='backspace':
                    response.append(number)
                #otherwise, take the last letter off the 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:  # at least one key was pressed
            response.keys.extend(theseKeys)  # storing all keys
            response.rt.append(response.clock.getTime())
        #check for quit
        if "escape" in theseKeys:
            endExpNow = True
        if response == '9999': # was this correct?
            correctAns = 1
        else: 
            correctAns = 0
        if theseKeys == 'enter':
            response.keys.extend(theseKeys) # storing all keys
            response.rt.append(attemptresponse.clock.getTime())
            if correctAns == 1:
                attempt += 888 #ends and goes to next problem
                currentProb += 1
                dataFile.write(attempt,attemptresponse,theseKeys,response,correctAns) #output separated by commas
                #dataFile.write('PID    COND    PROB    ATT TIME    RESPn')
                response_correct.draw()
                win.flip()
                event.waitKeys()
            if correctAns == 0:
                attempt += 1 #LB = was previously setting to 1 forever
                dataFile.write(attempt-1,attemptresponse,theseKeys,response,correctAns) #output separated by 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()

我无法编写完整的代码,但希望能指出一些事情,让您获得最大的成功。

获得回应

theseKeys = event.getKeys(keyList=['1', '2', '3', '4', '5', '6', '7', '8', '9', '0'])
if "escape" in theseKeys:

这里theseKeys只能包含keyList中的东西,所以"escape"永远不存在。使用"escape"扩展keyList,并可能使用稍后使用的"enter"。

if ('1', '2', '3', '4', '5', '6', '7', '8', '9', '0') in theseKeys:

theseKeys是一个列表,当你在列表中做"(x,y,z)"时,它会寻找一个元素(x,y,z),而不是x,y和z的任何出现。如果你有上面的keyList,你知道任何非转义响应都是其中之一,所以这是不必要的。也是如此

theseKeys = event.getKeys(keyList=['1', '2', '3', '4', '5', '6', '7', '8', '9', '0', 'escape'])
if "escape" in theseKeys:
    core.quit()  # assuming that psychopy.core is imported
elif "enter" in theseKeys:
    # Something something
else:
   response.append(theseKeys[0])  # OBS: pick the first response

循环

看起来您确实想要使用for循环,而不是while循环。使用for循环,不必跟踪当前循环数。所以不是

while currentProb <= nProblem:

进行

for currentProb in range(nProblem):

然后currentProb的增量自动发生,并且循环在应该的时候终止。如果可能的话,这会更优雅。

正在等待响应

我有点怀疑您是否希望在while循环中使用event.waitKeys()而不是event.getKeys(),如果您希望控制等待时间,请使用maxWait参数,如果您想要跟踪时间,则使用core.Clock()参数。如果你想制作动画,while循环是可以的,但如果不是,event.waitKeys()会更简单、更安全。

最后,当您尝试实现这一点时,请使用大量的print语句来检查theseKeys和其他变量的实际内容。这是调试和捕获gotchas的最佳方式。

相关内容

  • 没有找到相关文章

最新更新