我正在尝试为我的实验编写一个函数,该函数在中间的彩色矩形上方显示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
答案 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()
答案 1 :(得分: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()