其他分享
首页 > 其他分享> > 计算平均值

计算平均值

作者:互联网

Sample tests

import codewars_test as test
from solution import find_average

@test.describe("Fixed Tests")
def fixed_tests():
    @test.it('Basic Test Cases')
    def basic_test_cases():
        test.assert_equals(find_average([1, 2, 3]), 2)

 Solution 

def find_average(numbers):
    i = 0
    sum = 0
    for n in numbers:
        i = i + 1 #列表长度
        sum = sum + n #求和  
    return sum / i #求平均值

find_average([1, 2, 3])

 

Output: 

3

 

标签:平均值,average,find,numbers,计算,test,sum,def
来源: https://www.cnblogs.com/a001ai-es/p/16408571.html