Discuss / Python / 高阶函数练习1~3

高阶函数练习1~3

Jack

#1 Created at ... [Delete] [Delete and Lock User]

练习2

from functools import reduce


def prod(n):
    def ji(j):
        return sum(j)
    return reduce(ji,n)
    

print('3 * 5 * 7 * 9 =', prod([3, 5, 7, 9]))
if prod([3, 5, 7, 9]) == 945:
    print('测试成功!')
else:
    print('测试失败!')

Jack

#2 Created at ... [Delete] [Delete and Lock User]

我检查了一下,发现一个错误,修改一下

from functools import reduce


def prod(n):
    def ji(j,e):
        return j * e
    return reduce(ji,n)
    

print('3 * 5 * 7 * 9 =', prod([3, 5, 7, 9]))
if prod([3, 5, 7, 9]) == 945:
    print('测试成功!')
else:
    print('测试失败!')



  • 1

Reply