1

我正在尝试使用 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.\n\nType 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    RESP\n')
                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()
4

1 回答 1

1

我无法写出你的完整代码,但希望能指出一些能让你顺利完成的事情。

获得响应

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

这里theseKeys只能包含里面的东西,keyList所以“逃跑”永远不存在。使用“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循环在它应该终止时终止。如果可能,这会更加优雅。

等待回复

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

最后,当您尝试完成这项工作时,请使用大量print语句来检查theseKeys和其他变量的实际内容。这是调试和捕获问题的最佳方式。

于 2014-09-10T19:06:29.353 回答