心理:如何让参与者使用键盘上的特定键进行响应。蓝= F?

时间:2017-04-04 17:59:27

标签: python psychopy

目前我有代码:

import random
from psychopy import visual, event
win = visual.Window()

# A TextStim and five of each word-color pairs
stim = visual.TextStim(win)
pairs = 5 * [('blue', 'blue'), ('yellow', 'blue'), ('green', 'yellow'), ('red','red')]
random.shuffle(pairs)

# Loop through these pairs
for pair in pairs:
    # Set text and color
    stim.text = pair[0]
    stim.color = pair[1]

    # Show it and wait for answer
    stim.draw()
    win.flip()
    event.waitKeys()

我正在尝试分配密钥' f'在键盘上的颜色为红色,' g'蓝色,' h'黄色和' j'绿色,所以当参与者按下' f'当他们看到红色时,我想记录反应时间,我希望它能说出“正确”的反应。或者'不正确'?我已经尝试了很多代码,但我认为我把它们放错了顺序或者我错了!

1 个答案:

答案 0 :(得分:1)

一种巧妙的方法是使用python词典作为键义意义映射。所以在开始时,定义

answer_keys = {'f': 'red', 'g': 'blue', 'h': 'yellow', 'j': 'green'}

作为一个简单的演示,那么

answer_keys['f'] == 'blue'  # False
answer_keys['g'] == 'blue'  # True

要在实验中使用此功能,请执行以下操作:

# Get answer and score it, and get RT
key, rt = event.waitKeys(keyList=answer_keys.keys(), timeStamped=True)[0]  # Only listen for allowed keys. [0] to get just first and only response
score = answer_keys[key] == pair[1]  # Whether the "meaining" of the key is identical to the ink color of the word.

# Show feedback
stim.text = 'correct' if score else 'incorrect'  # show text depending on score
stim.text += '\nRT=%i ms' %(rt*1000)  # Add RT to text, just for demo
stim.draw()
win.flip()
event.waitKeys()  # Wait, just to leave it on the screen for a while.