不可修改的类型和替换输入

2024-06-25 23:18:03 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

有什么建议吗?你知道吗


Tags: 代码foriflabulinfllelif
1条回答
网友
1楼 · 发布于 2024-06-25 23:18:03
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)
))

,切片对象的三元组。expInfo是一个字典,只接受哈希类型和

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

引发错误

 TypeError: unhashable type: 'slice'

因为,它们不是hashable。像这样在片中使用字符串是非常非常奇怪的,所以我不认为你输入的键是正确的。你知道吗


我想你想做的是查字典。为此,您需要:

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

(请注意我为使其更具可读性而添加的空格。我建议在进一步使用python之前阅读PEP8,因为您将样式指南扔出了窗口)。你知道吗

相关问题 更多 >