我正在读一本Python书,我有一个问题是需要的功能:无法确定
Define a new function called "do_four" that takes a function object and a value and calls the function four times, passing the value as a parameter. There should be only two statements in the body of this function, not four.
我试过,但现在我想它应该打印16次什么,但应该有体2语句的每个功能。我的代码是
def hello1(a, g):
a(g)
a(g)
def hello2(ar):
print (ar)
print (ar)
hello1(hello2, "hello")
print("")
def do_four(a, g):
hello1(a, g)
hello1(a, g)
do_four(hello2, "hjh")
print("")
def hello3(a, g):
print (a, g)
print (a, g)
hello3(do_four, "aad")
和我得到这样的输出:
hello
hello
hello
hello
hjh
hjh
hjh
hjh
hjh
hjh
hjh
hjh
<function do_four at 0x1056a9840> aad
<function do_four at 0x1056a9840> aad
有人能解释什么,我缺少的,我怎么能做到这一点?
P.S. - 我现在不想使用循环。我正在学习功能。
“我不想使用循环现在“ - - 太糟糕了,因为循环是答案。 – user2357112
问题中没有什么说你不能使用循环,这是最简单,最合乎逻辑的方法,只用两行代码来解决问题。虽然您选择的答案中的函数只包含两行,但它们需要添加其他所有函数才能工作 - 我认为这违背了问题的精神(以及Python语言)。 – martineau