不可用的类型&替换输入

时间:2018-06-18 13:32:17

标签: python psychopy

目标很简单:显示带有选项的对话框,然后当选择一个选项时,它将自动替换一些值(以减少在手动键入值时出错的可能性)。以下代码是更大代码的一部分,但这是最重要的部分。更大的代码是由其他人编写的。我写了以下块:

if expInfo2['quadrant'] == 'UL':
        expInfo['refOrientation':'45','x':'-4.24','y':'4.24']
    elif expInfo2['quadrant'] == 'LL':
        expInfo['refOrientation':'-45','x':'-4.24','y':'-4.24']
    elif expInfo2['quadrant'] == 'UR':
        expInfo['refOrientation':'-45','x':'4.24','y':'4.24']
    elif expInfo2['quadrant'] == 'LR':
        expInfo['refOrientation':'45','x':'4.24','y':'-4.24']

代码正常工作,直到它读取第二行,并给出以下错误:

Traceback (most recent call last): File "D:\User\File\Experiment.py", line 48, in <module> expInfo['refOrientation':'45','x':'-4.24','y':'4.24'] TypeError: unhashable type ()

我对编程的经验是有限的,但我理解我在第二行中放置的东西并不合适。但是,我正在考虑将它们一块一块地拆分,但我认为这不会起作用,如下所示:

if expInfo2['quadrant'] == 'UL':
    expInfo['refOrientation':'45']
    expInfo['x':'-4.24']
    expInfo['y':'4.24']
et cetera...

完整代码:

#present a dialogue to chose lab room and whether eyetracker is on or not
expInfo2 = {'lab':'2','eyetracker': '0','quadrant':''}
dlg = gui.Dlg(title="Info", pos=(200, 400))
dlg.addField('Which lab are you in? 2 for lab-2, 3 for lab-3',expInfo2['eyelab'])
dlg.addField('Do you want the eyetracker on? 0 for yes, 1 for no',expInfo2['eyetracker'])
dlg.addField('What quadrant is used? UL=Upper Left, LL=Lower Left, UR=Upper Right, LR=Lower Right',expInfo2['quadrant'])
inf = dlg.show()

expInfo2['lab']=inf[0]
expInfo2['eyetracker'] = inf[1]
expInfo2['quadrant'] = inf[2]

############################## THIS IS THE CODE FOR LAB 2 ###########################################
if expInfo2['lab'] == '2':


    expInfo = {'observer':'insert','typeofstaircase':'insert','refOrientation':'','startorient':'insert','x':'','y':'','numstair':4,}
    dateStr = time.strftime("%b_%d_%H%M", time.localtime())#add the current time

    if expInfo2['quadrant'] == 'UL':
        expInfo['refOrientation':'45','x':'-4.24','y':'4.24']
    elif expInfo2['quadrant'] == 'LL':
        expInfo['refOrientation':'-45','x':'-4.24','y':'-4.24']
    elif expInfo2['quadrant'] == 'UR':
        expInfo['refOrientation':'-45','x':'4.24','y':'4.24']
    elif expInfo2['quadrant'] == 'LR':
        expInfo['refOrientation':'45','x':'4.24','y':'-4.24']

    #present a dialogue to change params
    dlg = gui.Dlg(title="Info", pos=(200, 400))
    dlg.addField('Observer:',expInfo['observer'])
    dlg.addField('Type of staircase?', expInfo['typeofstaircase'])
    dlg.addField('Start Orientation Increment:',expInfo['startorient'])
    dlg.addField('X:',expInfo['x'])
    dlg.addField('Y:',expInfo['y'])
    dlg.addField('Ref. Orienation:',expInfo['refOrientation'])
    #dlg.addField('Number of Staircases',expInfo['numstair'])
    inf = dlg.show()

    expInfo['observer']=inf[0]
    expInfo['typeofstaircase'] = inf[1]
    expInfo['startorient']=inf[2]
    expInfo['x']=inf[3]
    expInfo['y']=inf[4]
    expInfo['refOrientation']=inf[5]
    #expInfo['numstair'] = inf[6]
    #dlg = gui.DlgFromDict(expInfo, title='info', fixed=['date'])
    #if dlg.OK:
    #    print(expInfo)
    #else:
    #    core.quit()#the user hit cancel so exit

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

expInfo['refOrientation':'-45','x':'-4.24','y':'-4.24']

所以这被解释为

expInfo.__getitem__((
   slice('refOrientation', '-45', None),
   slice('x', '-4.24', None),
   slice('y', '-4.24', None)
))

,一个3元组的切片对象。 expInfo是一个字典,只采用可混合类型和

hash((
   slice('refOrientation', '-45', None),
   slice('x', '-4.24', None),
   slice('y', '-4.24', None)
))

引发错误

 TypeError: unhashable type: 'slice'

因为,他们不是hashable。在这样的切片中使用字符串是非常非常奇怪的,所以我不认为你正在以正确的方式输入密钥。

我认为你要做的是update字典。要做到这一点,你想要:

if expInfo2['quadrant'] == 'UL':
    expInfo.update({'refOrientation': '45', 'x': '-4.24', 'y': '4.24'})
elif expInfo2['quadrant'] == 'LL':
    ...

(请注意我放入的空白区域以使其更具可读性。我建议您先阅读PEP8然后再使用python进行阅读,因为您将样式指南抛出窗口。)