2016-02-27 282 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

這不是一個任務,老師給了我們這個任務嘗試它,我可以用一些幫助來修復錯誤 –

相關問題