为什么这种代码wars算法不能仅使用函数工作?

时间:2018-08-06 17:01:26

标签: python algorithm

我已经尝试过使用可视化工具,但是此问题仅返回一个。这是代码战问题:https://www.codewars.com/kata/calculating-with-functions/solutions/python

def zero(f = None): return 0 if not f else f(0)
def one(f = None): return 1 if not f else f(1)
def two(f = None): return 2 if not f else f(2)
def three(f = None): return 3 if not f else f(3)
def four(f = None): return 4 if not f else f(4)
def five(f = None): return 5 if not f else f(5)
def six(f = None): return 6 if not f else f(6)
def seven(f = None): return 7 if not f else f(7)
def eight(f = None): return 8 if not f else f(8)
def nine(f = None): return 9 if not f else f(9)

def plus(y): return lambda x: x+y
def minus(y): return lambda x: x-y
def times(y): return lambda  x: x*y
def divided_by(y): return lambda  x: x/y
four(plus(nine))

1 个答案:

答案 0 :(得分:1)

您需要致电nine();否则nine只是一个函数对象,不能与整数混合进行数字运算。

print(four(plus(nine())))

这将输出:

13