現在,當我檢查此函數時,在export_venues中,如果在費用中的invenue_types:TypeError:unhashable type:'list',我不確定我在這里做錯了什么。我確實創建了一個收入字典,以 Income_types 作為鍵,以 Income_value 作為值。迭代給定的收入字典,根據給定的收入類型(字符串列表)進行過濾,然后導出到文件。將給定收入類型從給定收入字典導出到給定文件。def export_incomes(incomes, income_types, file): final_list = [] for u, p in expenses.items(): if expense_types in expenses: final_list.append(':'.join([u,p]) + '\n') fout = open(file, 'w') fout.writelines(final_list) fout.close()如果這是應在 txt 中顯示的收入列表,如果用戶輸入股票、遺產、工作和投資,則每個項目和值應位于單獨的行上:庫存:10000房產:2000工作:80000投資:30000
1 回答

繁花如伊
TA貢獻2012條經驗 獲得超12個贊
首先,您的問題以支出開始,但以收入結束,代碼的參數中也有收入,所以我將選擇收入。
其次,錯誤說明了答案?!癳xpense_types(invenue_types)”是一個列表,“expenses(invenues)”是一個字典。您正在嘗試在字典中查找列表(不可散列)。
因此,要使您的代碼正常工作:
def export_incomes(incomes, income_types, file):
items_to_export = []
for u, p in incomes.items():
if u in income_types:
items_to_export.append(': '.join([u,p]) + '\n') # whitespace after ':' for spacing
with open(file, 'w') as f:
f.writelines(items_to_export)
如果我做出了任何錯誤的假設或誤解了您的意圖,請告訴我。
添加回答
舉報
0/150
提交
取消