我是计算像素点之间的距离,只是在程序中做了一个鼠标回调函数,用于计算x和y坐标。
这是我试过的代码,
def distance():
length = len(position)
# Distance in terms of x
distance_value = position[length-1][0] - position[length-2][0]
# Distance in terms of y
# distance_value = position[length-1][1] - position[length-2][1]
print("Value of pixel is: " + str(distance_value))
IndexError: list index out of range.
最佳答案
我想您需要执行以下操作:
def distance():
length = len(position)
# Distance in terms of x
try:
distance_value = position[length-1][0] - position[length-2][0]
print("Value of pixel is: " + str(distance_value))
except IndexError as e:
print('There is an error')
print(str(e))
关于python - 如何捕获 python 中所有索引超出范围的错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55593355/