我试图运行此代码:为什么我在这个Python代码中得到一个KeyError?
dictVar = {'PI': 3.14,
25: "The Square of 5",
"Weihan": "My Name"
}
print("The value corresponding to the key " + str(3.14) + " is: " + dictVar[3.14])
我不断收到以下错误:
Traceback (most recent call last):
File "C:/Users/KitKat#21266/Google Drive/Project Environment/From 0 to 1 Python Programming/Dictionary and If-Else.py", line 8, in <module>
print("The value corresponding to the key " + str(3.14) + " is: " + dictVar[3.14])
KeyError: 3.14
为什么会出现这种错误发生的呢?
错误是明确的,你有没有名为键'3.14',你有''PI'','25'和'‘胃寒’' – EdChum
请花一点时间来这里尝试一下代码格式化工具。对于非常短的代码,我们有'inline formatting',并且块的格式化。刚才有人为你重新格式化了你的文章,但是如果你能做到这一点,那么它可以节省工作的人。谢谢! – halfer
在dictVar 3.14中是与'PI'对应的值。这本身并不是一个关键。 –