计算平均值
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