问题描述
我尝试在 Spyder 中运行代码并收到这样的错误。但是我在 Anaconda 的 Spyder 中成功运行了代码
解决方法
它有所不同,但是当您尝试索引到容器中时通常会导致键错误,并且索引无效。
duck = {
'a': 11
'b': 22
'c': 33
}
# Note that 'd' is *NOT* a key into the dictionary..
print(duck['a']) # VALID
print(duck['b']) # VALID
print(duck['c']) # VALID
print(fuck['d']) # INVALID.... WRONG... BAD... DO NOT DO THIS
# There is no element having index 'd'
如果您得到 KeyError
,则表示方括号 []
内的某些内容是错误的。
您可能将无效索引传递到容器中。