gpt4 book ai didi

Python 运行时错误 : dictionary changed size during iteration

转载 作者:行者123 更新时间:2023-11-28 20:41:17 24 4
gpt4 key购买 nike

<分区>

我正在练习一个非常简单的 python 代码。我试图找到它的解决方案,但找不到。

def del_contacts():
for name, number in d1.items():
if (del_name == name):
del d1.name and print ("Contact deleted!")
else:
print ("Contact does not exist")

z = input ("Do you wish to delete any of the number you added? ")
if z == 'yes':
del_name = input ("Type the name of the contact you wish to delete")
del_contacts()
else:
print ("ok")

但这给了我错误 in <module> del_contacts()接着是 in del_contacts for name, numbers in d1.items(): RuntimeError: dictionary changed size during iteration

经常遇到这样的问题。谁能告诉我为什么会发生此错误?是否有任何修复以及我将来应该怎么做才能避免此类错误?

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com