Why does my Python program return "ValueError: math domain error"?

Why does my Python program return "ValueError: math domain error"?

我正在制作这个非常简单的程序,它计算玩家的坐标与另一个地方的坐标之间的距离(对于 Minecraft)。

import math
px = int(input("Your x-coordinate: "))
pz = int(input("Your z-coordinate: "))
x = int(input("X-coordinate of destination: "))
z = int(input("z-coordinate of destination: "))
dist = math.sqrt((px-x)^2+(pz-z)^2)
print("Distance is %d meters." % dist)

当我输入 (0, 0) 作为我的坐标和 (1, 1) 作为其他地方的坐标时,Python returns "ValueError: math domain error" 而不是 root 的期望值2. 虽然当我输入 (0, 0) 作为我的坐标和其他地方的坐标时,Python returns“0”。有人可以为我确定问题和可能的解决方案吗?

dist = math.sqrt((px-x)^2+(pz-z)^2)

^ symbol is used for the bitwise XOR operation. For taking power, you should use either math.pow()**,即

dist = math.sqrt((px-x)**2+(pz-z)**2)

或者,您也可以使用 math.hypot():

dist = math.hypot(px-x, pz-z)