如何检查循环内的准确性?

2024-10-06 13:41:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我试着为我的实验编码一个函数,它显示了2个标签,上面有一个彩色矩形。受试者必须按左键或右键,用上面的一个标签对颜色进行分类。你知道吗

我想在循环中编码,如果精确度=1,那么实验应该显示一个信息文本,说明他们的选择是正确的,反之亦然,如果精确度=0。在按enter键继续之后,它应该返回到原始循环,然后重复自身。你知道吗

我该怎么做?你知道吗

# make a function for one trial of colour practice
def con1_trial(self):
    global trial
    global key
    trial += 1
    target_colour = random.choice(colours) 

    # show one square with gouloboy colour in top right corner of screen
    col3rec.setFillColor(target_colour)
    col3rec.draw()
    sinij_text.draw()
    boy_text.draw()

    # draw and flip
    win.flip()

    key, test_answer = event.waitKeys(keyList=['right', 'left', 'escape'], timeStamped = True)[0]
    for colour_pair in colour_pairs:
        if test_colour == colours[0] and key == "left":
            accuracy = 1
        elif test_colour == colours[1] and key == "right":
            accuracy = 1
        elif key == 'escape':
            core.quit() 
        else: accuracy = 0

    # records time in ms
    rt = (test_answer - test_start)*1000
    return accuracy, rt

Tags: andkeyintestright编码for标签
2条回答

试用的定义是:挑选一种颜色,画一幅画,等待输入,然后记录答案。 这里有一些伪代码

def trial():
    color = random.choice(colours)
    # draw stuff
    # set time start
    # wait for key press
    # check if key press correct
    # print whether they were correct and the time
    if enter pressed: trial()

要使代码整洁,首先在脚本的前面定义一个反馈函数:

feedback_text = visual.TextStim(win)
def show_feedback(feedback):
    # Show feedback on screen
    feedback_text.text = feedback
    feedback_text.draw()
    win.flip()

    # Wait for key
    event.waitKeys()

在代码后面添加以下内容:

# Show feedback
if accuracy == 1:
    show_feedback('Correct! Well done. Press a key to continue...')
elif accuracy == 0:
    show_feedback('Wrong! Press a key to continue...')

。。。有正确的压痕。我总是有某种功能来显示信息。在show_feedback中,您还可以添加一些内容以退出实验:

key = event.waitKeys()[0]  # get first key pressed
if key == 'escape':
    core.quit()

相关问题 更多 >