我保留了我定义的函数,并且在运行程序时没有错误。问题是当我尝试调用函数时没有发生任何事情。
from math import sqrt
from recommendations import critics
def sim_distance(prefs, p1, p2):
si = {}
for item in prefs[p1]:
if item in prefs[p2]:
si[item] = 1
# If they have no ratings in common, return 0
if len(si) == 0:
return 0
# Add up the squares of all the differences
sum_of_squares = sum([pow(prefs[p1][item] - prefs[p2][item], 2) for item in prefs[p1] if item in prefs[p2]])
return 1 / (1 + sqrt(sum_of_squares))
reload (recommendations)
sim_distance(critics, 'Jack Matthews', 'Toby')
答案 0 :(得分:1)
您的函数正在返回,但您没有对返回的数据执行任何操作。您可以将返回值分配给变量x = sim_distance(...)
,然后对该变量执行某些操作。例如,print(x)