2017-03-15 114 views
0

我想为我的实验编写一个函数,在中间的彩色矩形上方显示2个标签。受试者必须按左侧或右侧以上述标签之一对颜色进行分类。如何检查循环内的精度?

我想在循环内编码,如果精度= 1,那么实验应该显示一个信息文本,说他们的选择是正确的,反之亦然,如果精度= 0。应该回到原来的循环,然后重复自己。

我该怎么做?

# 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 
+0

有一天人们会停止使用全局变量。我希望我能够活得足够长时间来见证那神圣恩典的时刻。 –

回答

0

为了让代码整洁,首先在脚本中的某处定义早期反馈功能:

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() 
+0

谢谢,这非常有用。 –

+0

虽然现在它完全冻结后,我已经编写了我的脚本中的定义和功能... –

0

试用定义为挑选颜色,绘图,等待输入,然后记录答案。 下面是一些伪

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()