【发布时间】:2021-08-15 19:16:48
【问题描述】:
os.chdir("Güvenlik_Hesaplar")
files = ''
dictionary = {}
ant = os.listdir()
dict_number = 1
i = 0
while i < len(ant): # If the variable 'i' is less than the length of my files
dictionary["{}" : ant[i].format(dict_number)] #e.g = '1': 'myfile.txt'
dict_number += 1
i += 1
错误:
File "C:\Users\Barış\Desktop\Python\prg.py", line 20, in passwordrequest
dictionary["{}" : ant[i].format(dict_number)]
TypeError: unhashable type: 'slice'
你能帮我解决这个问题吗?我正在使用 Windows x64
【问题讨论】:
-
您尝试插入
dictionary,键为dict_number,值为ant[i]?然后就做dictionary[str(dict_number)] = ant[i]。您也可以删除dict_number,因为它始终是i+ 1
标签: python dictionary typeerror python-3.9