问题描述
while stack.isEmpty() != 1:
fin = stack.pop()
print fin - output is (1,1)
k = final.get(fin)
return k
def directionToVector(direction, speed = 1.0):
dx, dy = Actions._directions[direction]
return (dx * speed, dy * speed)
directionToVector = staticmethod(directionToVector)
但是当我返回时,它给我一个错误,最后是我用键和值列表创建的目录
but when I do this return, it gives me an error and final is the directory that I have made with lists of keys and values
错误是:
File "line 212, in directionToVector
dx, dy = Actions._directions[direction]
KeyError: 'W'
推荐答案
Actions._directions
大概是字典,因此该行:
Actions._directions
is presumably a dictionary, so the line:
dx, dy = Actions._directions[direction]
在运行时(基于错误消息)是:
at runtime (based on the error message) is:
dx, dy = Actions._directions["W"]
,它抱怨该词典中没有键"W".因此,您应该检查一下是否确实已在其中添加了一些值的键.另外,您可以执行以下操作:
and it's complaining that there's no key "W" in that dictionary. So you should check to see that you've actually added that key with some value in there. Alternatively, you can do something like:
dx, dy = Actions._directions.get(direction, (0, 0))
其中(0,0)可以是没有此类键时选择的任何默认值.另一种可能性是显式处理该错误:
where (0, 0) can be any default value you choose when there's no such key. Another possibility is to handle the error explicitly:
try:
dx, dy = Actions._directions[direction]
except KeyError:
# handle the error for missing key
这篇关于执行python代码字典问题时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!