2016-02-27 279 views
-2

我真的不知道我出错的地方。你能帮我么?将代码上传到学校校正系统时,我不断收到此错误。NoneType'object has no attribute'items

-- test: 1 
    -- failed 

    -- standard error 
    Traceback (most recent call last): 
    File "/proc/self/fd/3", line 6, in <module> 
     print(sorted(new_dict.items())) 
    AttributeError: 'NoneType' object has no attribute 'items' 
    -- 
    -- exit code: 1 
       (anything other than 0 indicates an error) 
       (all scripts are killed after three seconds) 

    -- expected standard output 
    [(4, 'a'), (10, 'c')] 
    -- 

    -- actual standard output (max 200 lines) 
    [(4, 'a'), (10, 'c')] 
    -- 

我的代码是:

import sys 

def swap_unique_keys_values(n): 
    d = {} 
    for i in n: 
     if not n[i] in d: 
      d[n[i]] = i 
     else: 
      del d[n[i]] 
    print(sorted(d.items())) 

def main(): 
    swap_unique_keys_values(sys.argv[1]) 

if __name__ == '__main__': 
    main() 

的输入是一本字典。任何帮助真的很感激。

+3

回溯返回值不符合您的代码。 – vaultah

回答

1

该错误几乎总是意味着您忘记从方法返回值。

其他一些方法称为您的方法,保存返回的值,然后尝试对其进行操作。蟒蛇说,“不,没有相应的价值在那里,它只是无”

尝试在swap_unique_keys

+0

当我尝试返回一个值,它修复了错误,但是当我尝试返回排序(d.items())它说列表对象没有属性项目 –

+0

不幸的是,那么,我认为这是家庭作业的一部分!祝你好运:) –

+0

这不是一个任务,老师给了我们这个任务尝试它,我可以用一些帮助来修复错误 –

相关问题