如何在while循环中修复'NoneType'对象不可订阅'错误

问题描述 投票:1回答:1

Windows 10 Python 3.7 Anaconda 1.9.7 Spyder 3.3.3 PsychoPy for Python 2.7

我正在编写一个实验,需要以随机顺序呈现图像以供参与者响应。我能够在一个数组中获取图像,但是为了一次呈现它们,我使用的是一个while循环,每次循环时变量都会增加1。它没有将变量识别为数字,因此数组不能调用任何东西。

我试过不随机化变量,看看是不是问题,但它似乎是我的变量i不被读作数字

#import packages
import random, os
from psychopy import core, visual, event
from PIL import Image

#setup screen with specs and draw
win = visual.Window([400, 300], monitor="testMonitor")
message = visual.TextStim(win, text="")

message.draw()
win.flip()
core.wait(3.0)

#set image size and populate array with images
stim_size = (0.8, 0.8)
image = [i for i in os.listdir('C:/Users/*/psychopy-tests') 
                    if i.endswith('.bmp')]
#randomize image order
images = random.shuffle(image)

this is where my issue seems to be

i = 0
while i != 29: #there are only 28 images

    new = images[i] #this is where the issue is
    image_stim = Image.open(new)

    stim = visual.ImageStim(win, image_stim, size = (stim_size))
    stim.draw()
    win.update()
    output = []
    if event.getKeys(keyList=['space']):
        output[i] = 1
    if event.getKeys(['escape']):
        win.close()
        core.quit()
    if event.getKeys(keyList=None):
        output[i] = 0
        core.wait(5.0)
    i = i + 1
python-2.7 while-loop anaconda psychopy
1个回答
2
投票

random.shuffle在适当的地方洗牌并且不返回任何东西,即它返回None。

因此images是无,不可订阅。

source

© www.soinside.com 2019 - 2024. All rights reserved.