首页 > 解决方案 > 使用另一个文件中的函数进行计算

问题描述

我正在尝试使用另一个文件中的函数进行计算。我有一个脚本,我想通过使用另一个文件中的函数来计算平均值,我已经收到了特定文本的字数。我想在我当前的脚本中使用这个数字来计算平均值。我尝试使用的功能如下所示:

def Word(liste):
    test =(len(liste))

我尝试添加它来计算平均值如下所示:

def mean(liste, Word):
        
    Sum = (sum(liste))
    result = Sum/Word
    print(result)

不幸的是,我总是收到以下错误:

TypeError: unsupported operand type(s) for /: 'float' and 'function'

但是如何将函数更改为整数?这甚至可能吗?

标签: pythonfunctionmean

解决方案


在 word 函数中添加 return 语句如下

def Word(liste):
    test =(len(liste))
    return test

并表示功能修改

result = Sum/Word(liste)

推荐阅读