如何在不使用sum函数的情况下求平均值



下面是我的代码

def average(a_list):  # returns average value of a list of numbers.
total = 0
a_list = []
for n in (a_list):
total += n
return total/ len(a_list)

但是我总是得到一个错误,我的"len(a_list)"不能工作,因为它除以0

是的,它是零:您在进入循环之前非常明确地销毁了输入参数。不要!

删除语句:

def average(a_list):  # returns average value of a list of numbers.
total = 0
for n in a_list:
total += n
return total / len(a_list)

最新更新