# 求指点下这段Python代码怎么改错？

582 次浏览

import math

#两点坐标距离公式
def sqrtpowsum(A,B):
return math.sqrt(math.pow(A[0]-B[0],2)+math.pow(A[1]-B[1],2)+math.pow(A[2]-B[2],2))

#型材拉伸量
L0e = 0.1

#初始点A坐标
Coordinate_A = [16.6588, 14.0349, 0.0]

#初始点A'坐标
Coordinate_A_quote = [16.6588, 14.0349, 300]

#每一轮点B坐标
Coordinate_B = [
[16.6603, 14.0594, -4.86946],
[16.6662, 14.1329, -9.72499],
[16.6763, 14.2555, -14.5686]
]

""" ========================
分割线下方内容请勿修改
========================
"""

#点A的三个坐标
A_x = Coordinate_A[0]
A_y = Coordinate_A[1]
A_z = Coordinate_A[2]

#点A'的三个坐标
A_quote_x = Coordinate_A_quote[0]
A_quote_y = Coordinate_A_quote[1]
A_quote_z = Coordinate_A_quote[2]

#计算点B的个数
n = len(Coordinate_B)

#该轮点B'的坐标
Coordinate_B_quote =[]

for i in range(n):
# 该轮B点的三个坐标
B_x = Coordinate_B[i][0]
B_y = Coordinate_B[i][1]
B_z = Coordinate_B[i][2]
# 计算L和l
L = sqrtpowsum(Coordinate_A,Coordinate_A_quote) +L0e
l = sqrtpowsum(Coordinate_A,Coordinate_B)
m = L/l
# 计算该轮点B'的三个坐标
B_quote_x = m*B_x + (1-m)*A_x
B_quote_y = m*B_y + (1-m)*A_y
B_quote_z = m*B_z + (1-m)*A_z
# 该轮点B’的坐标
Coordinate_B_quote[0] = B_quote_x
Coordinate_B_quote[1] = B_quote_y
Coordinate_B_quote[2] = B_quote_z
# 迭代
Coordinate_A = Coordinate_B
Coordinate_A_quote = Coordinate_B_quote
# 输出该轮B'坐标
print("B'" + i + "=" + Coordinate_B_quote +"\n")

Traceback (most recent call last):
File "E:/python code/trace code.py", line 44, in <module>
l = sqrtpowsum(Coordinate_A,Coordinate_B)
File "E:/python code/trace code.py", line 5, in sqrtpowsum
return math.sqrt(math.pow(A[0]-B[0],2)+math.pow(A[1]-B[1],2)+math.pow(A[2]-B[2],2))
TypeError: unsupported operand type(s) for -: 'float' and 'list'

2017-11-15 提问
2 个回答

1.Coordinate_B是二维数组，Coordinate_A坐标的值怎么能和列表做加减呢
2.Coordinate_B_quote =[]没有定义长度，不能Coordinate_B_quote[0] = B_quote_x这样赋值
3.print("B'" + i + "=" + Coordinate_B_quote +"n")需要转换字符串类型

import math

#两点坐标距离公式
def sqrtpowsum(A, B):
return math.sqrt(sum((a-b)**2 for a, b in zip(A, B)))

#型材拉伸量
L0e = 0.1

#初始点A坐标
Coordinate_A = [16.6588, 14.0349, 0.0]

#初始点A'坐标
Coordinate_A_quote = [16.6588, 14.0349, 300]

#每一轮点B坐标
Coordinate_B_lst = [
[16.6603, 14.0594, -4.86946],
[16.6662, 14.1329, -9.72499],
[16.6763, 14.2555, -14.5686]
]

for i, Coordinate_B in enumerate(Coordinate_B_lst):
# 计算L和l
L = sqrtpowsum(Coordinate_A, Coordinate_A_quote) +L0e
l = sqrtpowsum(Coordinate_A, Coordinate_B)
m = L/l
# 计算该轮点B'的三个坐标
Coordinate_B_quote = [m*b+(1-m)*a for a, b in zip(Coordinate_A, Coordinate_B)]
# 迭代
Coordinate_A, Coordinate_A_quote = Coordinate_B, Coordinate_B_quote
# 输出该轮B'坐标
print("B'" + str(i) + "=" + str(Coordinate_B_quote) +"\n")