【问题标题】:I am trying to create a cal_average function however I keep getting the syntax error that python cannot assign to function call我正在尝试创建一个 cal_average 函数,但是我不断收到 python 无法分配给函数调用的语法错误
【发布时间】:2020-05-31 09:01:02
【问题描述】:

Image of code

我正在尝试创建一个 cal_average 函数,请参阅下面的代码块:

def cal_average(numbers):
       sum(list_name) = sum(numbers)
       len(list_name) = len(numbers)
       ave(list_name) = sum(list_name)/len(list_names)
       return ave(list_name)

enter code hereresult = cal_average([3,4,5])

【问题讨论】:

  • sum(list_name) 是对名为 list_name 的变量上的函数 sum 的调用。在那个位置你真正需要的是一个变量名,例如sum_numbers = sum(numbers)。这同样适用于接下来的两行。
  • 请勿发布代码图片。

标签: python list function dictionary tuples


【解决方案1】:

错误很明显,你不能分配给函数调用,在python中sum是一个内置函数,len是一个内置函数,你可以使用statistics.mean

import statistics

statistics.mean([3, 4, 5])

输出:

4

或者,如果你想保留你的功能:

def cal_average(numbers):
       return sum(numbers) / len(numbers)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-27
    • 1970-01-01
    相关资源
    最近更新 更多