You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python实现Euclidean Distance计算 函数无输出无报错问题排查

问题描述

运行用于计算欧几里得距离的代码时,程序既不输出任何内容,也不抛出错误,执行完成后直接结束。原始代码如下:

lst_a=[1,2,3,4,5]
lst_b=[1,2,4,5,8]

def distance(lst_a, lst_b):
    sum_of = 0
    for x, y in zip(lst_a, lst_b):
        ans = (x - y)**2
        sum_of += ans
    return (sum_of)**(1/2)
故障原因

代码仅完成了distance函数的定义,没有主动调用函数,也没有打印函数返回的计算结果。Python解释器执行代码时,只会按顺序运行顶层语句:完成两个列表的赋值、完成函数对象的创建,不会自动执行函数内部的逻辑,因此运行全程不会产生任何输出,也不会触发报错。

修复方法

在代码末尾补充函数调用、结果打印的逻辑即可,修复后的完整可运行代码:

lst_a=[1,2,3,4,5]
lst_b=[1,2,4,5,8]

def distance(lst_a, lst_b):
    sum_of = 0
    for x, y in zip(lst_a, lst_b):
        ans = (x - y)**2
        sum_of += ans
    return (sum_of)**(1/2)

# 调用函数并打印结果
euclidean_res = distance(lst_a, lst_b)
print(euclidean_res)

运行上述代码后,会正确输出两个列表的欧几里得距离计算结果:3.3166247903554


内容的提问来源于stack exchange,提问作者Python_bug

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.27 02:39:09