在函数中使用多个if语句

时间:2015-03-02 00:58:10

标签: python excel element

这是我的代码:

ac = a + c
bd = b + d

indices = random.sample(range(len(ac)),len(ac))
ac = list(map(ac.__getitem__, indices))
bd = list(map(bd.__getitem__, indices))


with open('DD{}.csv'.format(fileName), 'w') as f:
    writer = csv.writer(f, delimiter=',')
    writer.writerow(['trial','target location',' source target',' source distractor','target','rt'])

    for i in range(len(ac)):

        loc = [1, 2]
        location = random.choice(loc)
        if location == 1:
            pos1 = [.05,.05]
            pos2 = [-.05, -.05]
        else:
            pos1 = [-.05, -.05]
            pos2 = [.05, .05]

        distractorstim = visual.ImageStim(win=win, pos=pos1, size=[0.5,0.5])
        distractorstim.autoDraw = True
        targetstim = visual.ImageStim(win=win, pos=pos2, size=[0.5,0.5])
        targetstim.autoDraw = True

        targetstim.image = ac[i]
        distractorstim.image = bd[i]
        win.flip()
        trialClock.reset()
        core.wait(.05)
        rt = trialClock.getTime()
        for el in ac:
            if el in a:
                target = 'congruent'
            else:
                target = 'incongruent'



    writer.writerow([i,location,ac[i],bd[i],target,rt])

所以这段代码的作用就是将一堆刺激信息记录到excel文件中。但是,当我尝试使用元素功能(' if el')来记录来自列表' a'的数据时,它只会重复记录最后一个值。如果我移动元素函数,我可以让它记录每个试验,但它只记录所有其他所有的最后一个值。知道如何解决这个问题吗?任何建议将不胜感激。 :)

2 个答案:

答案 0 :(得分:2)

在最后一个for循环中,在每个循环迭代中设置target,因此只有最后一次迭代才能确定目标的值。

可能是这样的:

target = None
for el in ac:
    if el in a:
        target = 'congruent'
if target is None:
        target = 'incongruent'

答案 1 :(得分:0)

更改:

 for el in ac:
        if el in a:
            target = 'congruent'
        else:
            target = 'incongruent'

为:

    if ac[i] in a:
        target = 'congruent'
    else:
        target = 'incongruent'

似乎要做的伎俩,谢谢所有评论的人。 :)