2017-03-15 57 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()