math.dist 接受列表还是整数?
Does math.dist take a list, or an integer?
我正在编写一些如下所示的代码:
import math
def get_coord_distance():
c_1 = input('What is the coordinate of the first point?\n').split(',')
c_2 = input('What is the coordinate of the second point?\n').split(',')
p = []
q = []
p.append(c_1)
q.append(c_2)
coordinate_distance = math.dist(p, q)
if p == q:
print('The two points equal each other. The distance is 0.')
exit()
return coordinate_distance
res = get_coord_distance()
print(res)
math.dist 使用两个列表,并使用距离公式并输出两个列表之间的距离。这看起来像:
p = [3, 14]
q = [1, 59]
print(math.dist(p, q))
这将输出这样的浮点数:
45.04442251822083
当我 运行 上面的代码(第一个代码块)时,我收到一条错误消息。
TypeError: must be real number, not list
我这里的错误是什么?我认为这是数学模块的错误,但我不确定。
你错了两件事。首先,您需要转换输入。其次,您不应该附加到 p & q,而是扩展。试试下面的代码
请注意不要扩展空列表。我已经使用map
函数获取转换后的坐标并直接赋值给P&Q
import math
def get_coord_distance():
c_1 = input('What is the coordinate of the first point?\n').split(',')
c_2 = input('What is the coordinate of the second point?\n').split(',')
p = map(int, c_1)
q = map(int, c_2)
coordinate_distance = math.dist(p, q)
if p == q:
print('The two points equal each other. The distance is 0.')
exit()
return coordinate_distance
res = get_coord_distance()
print(res)
对于像我这样遇到此错误的人:
AttributeError: module 'math' has no attribute 'dist'
你也可以这样计算两点之间的距离:
import math
def get_coord_distance():
a=input("What is the coordinate of the first point : ")
p1 = a.split(",")
b=input("What is the coordinate of the second point : ")
p2 = b.split(",")
coordinate_distance = math.sqrt( ((int(p1[0])-int(p2[0]))**2)+((int(p1[1])-int(p2[1]))**2) )
return coordinate_distance
res = get_coord_distance()
print(res)
我正在编写一些如下所示的代码:
import math
def get_coord_distance():
c_1 = input('What is the coordinate of the first point?\n').split(',')
c_2 = input('What is the coordinate of the second point?\n').split(',')
p = []
q = []
p.append(c_1)
q.append(c_2)
coordinate_distance = math.dist(p, q)
if p == q:
print('The two points equal each other. The distance is 0.')
exit()
return coordinate_distance
res = get_coord_distance()
print(res)
math.dist 使用两个列表,并使用距离公式并输出两个列表之间的距离。这看起来像:
p = [3, 14]
q = [1, 59]
print(math.dist(p, q))
这将输出这样的浮点数:
45.04442251822083
当我 运行 上面的代码(第一个代码块)时,我收到一条错误消息。
TypeError: must be real number, not list
我这里的错误是什么?我认为这是数学模块的错误,但我不确定。
你错了两件事。首先,您需要转换输入。其次,您不应该附加到 p & q,而是扩展。试试下面的代码
请注意不要扩展空列表。我已经使用map
函数获取转换后的坐标并直接赋值给P&Q
import math
def get_coord_distance():
c_1 = input('What is the coordinate of the first point?\n').split(',')
c_2 = input('What is the coordinate of the second point?\n').split(',')
p = map(int, c_1)
q = map(int, c_2)
coordinate_distance = math.dist(p, q)
if p == q:
print('The two points equal each other. The distance is 0.')
exit()
return coordinate_distance
res = get_coord_distance()
print(res)
对于像我这样遇到此错误的人:
AttributeError: module 'math' has no attribute 'dist'
你也可以这样计算两点之间的距离:
import math
def get_coord_distance():
a=input("What is the coordinate of the first point : ")
p1 = a.split(",")
b=input("What is the coordinate of the second point : ")
p2 = b.split(",")
coordinate_distance = math.sqrt( ((int(p1[0])-int(p2[0]))**2)+((int(p1[1])-int(p2[1]))**2) )
return coordinate_distance
res = get_coord_distance()
print(res)