修改列表中 TextStim 的随机顺序/查找循环中迭代的索引(Psychopy)

Randomising order of TextStim in an amended list/ finding the index of an iteration in a loop (Psychopy )

言归正传,我已经将 120 个不同颜色的 TextStim(不同颜色的单词)项目修改为一个名为 'trials' 的列表。我将循环 'trials' 来逐一呈现这些刺激。但是,它们已根据我使用的循环按顺序修改到此列表。理想情况下,我需要将它们随机化,以便在它们出现时使用。 我试过:

随机导入

试验 = random.shuffle(试验)

但我得到的只是 TypeError: 'NoneType' object is not iterable... 我认为这与列表中的刺激类型存储为错误的变量类型有关。出于同样的原因,当我尝试查找每个演示文稿的试用号时(对于试用中的 c:... trialnum = len(c))以便我可以将试用号与响应一起存储,但我没有收到有关它的消息以这种形式可迭代。基本上我觉得这两个问题在某种根本上是相关的。

如有任何帮助,我们将不胜感激

谢谢!

这是因为 random.shuffle 洗牌和 returns None(这就是为什么你会收到有关 NoneType 的错误),所以

random.shuffle(pairs)

而不是

pairs = random.shuffle(pairs)

作为一般性评论,您不会生成很多 TextStim,而是生成一个,然后在 运行 实验时更新它。看起来你正在做一个 Stroop 实验或类似的东西。所以做这样的事情:

# General setup
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'), ('red', '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()