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

如何解决Python AssertionError:无法找到'distance'定义?

解决"Unable to find definition for 'distance'"报错的方案

这个报错的核心是自动评分系统无法在你的提交代码中找到名为distance的顶级函数,以下是针对性的解决步骤:

  • 确认函数所在文件正确
    评分脚本从self.submission模块加载函数,意味着你需要把distance函数写在评分系统指定的提交文件中(比如常见的submission.py),而不是其他自定义命名的文件里。

  • 检查函数名完全匹配
    确保你的函数名是全小写的distance,没有拼写错误(比如少写字母、大小写不一致),评分系统的测试代码严格按照这个名称查找函数,任何拼写差异都会导致找不到。

  • 确保函数是顶级定义
    不要把distance函数嵌套在其他函数、类或者缩进块内部,必须直接定义在模块的最外层,比如:

    # 正确写法:顶级函数
    import numpy as np
    
    def distance(x0, x1):
        distance = np.sqrt((x0-x1).T @ (x0-x1))
        return distance
    

    错误示例(嵌套在类里):

    import numpy as np
    
    class MyClass:
        # 这里的distance是类方法,外部模块无法直接访问
        def distance(x0, x1):
            distance = np.sqrt((x0-x1).T @ (x0-x1))
            return distance
    
  • 补充必要的导入
    你的函数用到了numpy,必须在文件开头添加import numpy as np,否则即使函数被找到,运行时也会报NameError: name 'np' is not defined的错误。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.19 09:47:41