0
所以我有這個文件使用這些值:KeyError異常在Python 3詞典,但它返回當前值
AFG,13,0,0,2
ALG,15,5,2,8
ARG,40,18,24,28
存入字典是這樣的:
{'ARG': (40, 18, 24, 28), 'ALG': (15, 5, 2, 8), 'AFG': (13, 0, 0, 2)}
我有一個功能,讓用戶在關鍵字中打孔,並且它應該返回包含數字的元組。
但是,如果我是輸入,說,AFG,我得到:
Traceback (most recent call last):
File "C:\Users\username\Dropbox\Programming\Python\Project3.py", line 131, in <module>
main()
File "C:\Users\username\Dropbox\Programming\Python\Project3.py", line 110, in main
findMedals(countryDictionary, MedalDictionary)
File "C:\Users\username\Dropbox\Programming\Python\Project3.py", line 88, in findMedals
answer.append([medalDict[medalCount]])
KeyError: (13, 0, 0, 2)
正如你所看到的,KeyError異常給出了輸入鍵的正確值,但它爲什麼還在抱怨關於它?不KeyError是否意味着密鑰不存在?
我的代碼:
def findMedals(countryDict, medalDict):
search_str = input('What is the country you want information on? ')
for code, medalCount in medalDict.items():
if search_str in code:
answer.append([medalDict[medalCount]])
else:
answer = ['No Match Found']
我改變了代碼 如果SEARCH_STR代碼: 答案.append([medalCount]) else: answer = ['找不到匹配']' 現在我得到這個錯誤: TypeError:'builtin_function_or_method'對象不是iterable 我知道,通常顯示時,函數調用缺少括號,但它似乎我有我需要的? – user1768884