2017-09-08 54 views
-1

錯誤在用戶輸入後,總是告訴我set對象沒有屬性。我看過其他問題,但在我的情況下沒有取得任何成功。我嘗試了很多東西。基本上,該程序有一個我創建的密碼,我想將用戶的字符替換爲我擁有的密碼字母表。順便說一句,我已經用保密的目的替換了翻譯。希望有人能幫我解決這個問題。Python 2.7錯誤:'set'對象沒有屬性'__GETITEM__'在有raw_input條件後

cache_list = { 
'result' 
} 

usr = raw_input("Usr: ") 



if 'a' in usr: 
result1 = usr.replace('a', "hash1" , 1000000000) 
cache_list['result'].append(result1) 


if 'b' in usr: 
    result2 = usr.replace('b', "hash2" , 1000000000) 
    cache_list['result'].append(result2) 

if 'c' in usr: 
cache_list['result3'] = usr.replace('c', "hash3" , 1000000000) 
cache_list['result'].append(result3) 


if 'd' in usr: 
result4 = usr.replace('d', "hash4" , 1000000000) 
cache_list['result'].append('result4' 
           ) 


if 'e' in usr: 
result5 = usr.replace('e', "hash5" , 1000000000) 
cache_list['result'].append(result5) 


if 'f' in usr: 
result6 = usr.replace('f', "hash6" , 1000000000) 
cache_list['result'].append(result6) 

if 'g' in usr: 
result7 = usr.replace('g', "hash7" , 1000000000) 
cache_list['result'].append(result7) 


if 'h' in usr: 
result8 = usr.replace('h', "hash8" , 1000000000) 
cache_list['result'].append(result8) 


if 'i' in usr: 
result9 = usr.replace('i', "hash10" , 1000000000) 
cache_list['result'].append(result9) 


if 'j' in usr: 
result10 = usr.replace('j', "hash11" , 1000000000) 
cache_list['result'].append(result10) 


if 'k' in usr: 
result11 = usr.replace('k', "hash12" , 1000000000) 
cache_list['result'].append(result11) 


if 'l' in usr: 
result12 = usr.replace('l', "hash13" , 1000000000) 
cache_list['result'].append(result12) 


if 'm' in usr: 
result13 = usr.replace('m', "hash14" , 1000000000) 
cache_list['result'].append(result13) 


if 'n' in usr: 
result14 = usr.replace('n', "hash15" , 1000000000) 
cache_list['result'].append(result14) 


if 'o' in usr: 
result15 = usr.replace('o', "hash17" , 1000000000) 
cache_list['result'].append(result15) 


if 'p' in usr: 
result16 = usr.replace('p', "hash18" , 1000000000) 
cache_list['result'].append(result16) 



if 'q' in usr: 
result17 = usr.replace('q', "hash19" , 1000000000) 
cache_list['result'].append(result17) 


if 'r' in usr: 
result18 = usr.replace('r', "hash20" , 1000000000) 
cache_list['result'].append(result18) 



if 's' in usr: 
result19 = usr.replace('s', "hash21" , 1000000000) 
cache_list['result'].append(result19) 




if 't' in usr: 
result20 = usr.replace('t', "hash22" , 1000000000) 
cache_list['result'].append(result20) 




if 'u' in usr: 
result21 = usr.replace('u',"hash23" , 1000000000) 
cache_list['result'].append(result21) 



if 'v' in usr: 
result22 = usr.replace('v',"hash24" , 1000000000) 
cache_list['result'].append(result22) 



if 'w' in usr: 
result23 = usr.replace('w',"hash26" , 1000000000) 
cache_list['result'].append(result23) 




if 'x' in usr: 
result24 = usr.replace('x',"hash27" , 1000000000) 
cache_list['result'].append(result24) 





if 'y' in usr: 
result25 = usr.replace('y',"hash28" , 1000000000) 
cache_list['result'].append(result25) 





if 'z' in usr: 
    result26 = usr.replace('z',"hash29" , 1000000000) 
cache_list['result'].append(result26) 





if ' ' in usr: 
result27 = usr.replace(' ', "space" , 1000000000) 
cache_list['result'].append(result27) 



print cache_list[result] 
+0

什麼是您所遇到的問題?你看到了什麼行爲?你期望看到什麼? – Engineero

回答

2

你(可能是意外)創建一個初始化的變量cache_list設置時,而不是一本字典。嘗試使用:

cache_list = {"result": ""} 

這將創建一個在字典中一個關鍵result,並將其值設置爲""(空字符串)。

這裏是一個控制檯例如:

>>> cache_list = {"result"} 
>>> type(cache_list) 
<type 'set'> 

這組不具有__GETITEM__功能解釋抱怨。但是用我建議的初始化會得到正確的類型:

>>> cache_list = {"result": ""} 
>>> type(cache_list) 
<type 'dict'> 
>>> cache_list["result"] 
'' 

摘要:{"result"}創建設置,但{"result": ""}創建字典

0

既然你想在清單上存儲的cache_list應該是這樣的:

cache_list = {"result": []} 

usr = raw_input("Usr: ") 

if 'a' in usr: 
    result1 = usr.replace('a', "hash1", 1000000000) 
    print result1 
    cache_list['result'].append(result1) 

if 'b' in usr: 
    result2 = usr.replace('b', "hash2", 1000000000) 
    cache_list['result'].append(result2) 

print cache_list["result"] 
相關問題