列表中的平均数

时间:2019-07-02 12:58:47

标签: python function math sum average

找到5个数字 查找数字的平均值

#import statistics
print("enter 5#s")
num1 = int(input())
num2 = int(input())
num3 = int(input())
num4 = int(input())
num5 = int(input())
numbers = [num1, num2, num3, num4, num5]
numsum = sum(numbers)
print("sum is:", numsum)
#Above list is working
#fix below
def Average(numbers):
    return sum(numbers) / len(numbers)
print("AVG = ", round(Average, 2))

错误 追溯(最近一次呼叫过去): 文件“ main.py”,位于第15行 print(“ AVG =”,round(Average,2)) TypeError:类型函数santnt define_round_method

2 个答案:

答案 0 :(得分:0)

您需要在末尾添加额外的一行,该行应为:

Average(numbers)

答案 1 :(得分:0)

最后一行(对proxy: secretToken: "7891e9825fde46bee0ac2de2961a256909a7233c68b5893d863c3122168332e3" hub: db: type: sqlite-memory singleuser: image: name: jupyter/minimal-notebook tag: 2343e33dec46 profileList: - display_name: "Minimal environment" description: "To avoid too much bells and whistles: Python." default: true storage: extraVolumes: - name: jupyterhub-shared persistentVolumeClaim: claimName: nfs-claim1 extraVolumeMounts: - name: jupyterhub-shared mountPath: "/mnt/jovyan/shared" 的调用)

print

需要不缩进(向左移动,在函数外部),并且需要使用def Average(numbers): return sum(numbers) / len (numbers) print("AVG = ", round(Average, 2)) 作为参数来调用函数,如下所示:

numbers

您的代码可以得到简化和改进,也许像这样:

def Average(numbers):
    return sum(numbers) / len (numbers)
print("AVG = ", round(Average(numbers), 2))

样品运行:

num_list = []
for i in range(5):
    s = input('Enter the {}. number: '.format(i+1))
    n = int(s)
    num_list.append(n)

the_sum = sum(num_list)
print('Sum:', the_sum)

the_avg = round(the_sum / len(num_list), 2)
print('Avg:', the_avg)