以下是给出错误的代码:
from math import sqrt
def normal(vector):
sum = 0
for i in vector:
sum = sum + (i**2)
norm = sqrt(sum)
return round(norm, 4)
def innerproduct(vector_x, vector_y):
sum = 0
for i in vector_x:
for j in vector_y:
sum = sum + (i*j)
inner_product = sum
return inner_product
def distance(vector_x, vector_y):
distance = 0
for i in vector_x:
for j in vector_y:
dist = normal(i)**2 + normal(j)**2 - (2*(innerproduct(i,j)))
return sqrt(dist)
前两个函数运行正常,但第三个函数返回TypeError:'int'对象不可迭代。
答案 0 :(得分:0)
我得到的错误是:
Traceback (most recent call last):
File "t.py", line 25, in <module>
print(distance([1,2],[5,6]))
File "t.py", line 22, in distance
dist = normal(i)**2 + normal(j)**2 - (2*(innerproduct(i,j)))
File "t.py", line 5, in normal
for i in vector:
TypeError: 'int' object is not iterable
因为在函数normal
中,您对int(vector
)进行了for循环。