2016-04-15 58 views
0

我正在读一本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. - 我现在不想使用循环。我正在学习功能。

+5

“我不想使用循环现在“ - - 太糟糕了,因为循环是答案。 – user2357112

+1

问题中没有什么说你不能使用循环,这是最简单,最合乎逻辑的方法,只用两行代码来解决问题。虽然您选择的答案中的函数只包含两行,但它们需要添加其他所有函数才能工作 - 我认为这违背了问题的精神(以及Python语言)。 – martineau

回答

0

尝试使用此代码:

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, "hello1") 
print("") 


def do_eight(sf): 
    print(sf) 
    print(sf) 
do_four(do_eight, "hello2") 

def do_eight1(a,g): 
    do_four(a,g) 
    do_four(a,g) 

do_eight1(do_eight, "hello3") 

def do_sixteen(add): 
    print(add) 
    print(add) 

do_eight1(do_sixteen, "hello4") 

def do_sixteen1(a,g): 
    do_eight1(a,g) 
    do_eight1(a,g) 

do_sixteen1(do_sixteen, "hello5") 
+0

谢谢!其实你的拼写功能名称错误像四将八和八将十六,但它确定我现在得到答案:)) – user325923

7

什么说明要求是这样的

def some_func(arg): 
    print(arg) 

def do_four(fun, arg): 
    for _ in range(0, 4): 
     fun(arg) 

,然后它被称为像

do_four(some_func, "foo") 

该解决方案使用一个循环。不知道为什么你有一个厌恶环路,即明确所需要给出的描述

There should be only two statements in the body of this function, not four.

如果你真的想没有循环来做到这一点(我真的不明白为什么),然后do_four看起来像

def do_four(fun, arg): 
    fun(arg) 
    fun(arg) 
    fun(arg) 
    fun(arg) 

编辑

如果你真的想这样做没有循环,并希望每个函数只有2语句的话,你可以做...

def do_two(fun, arg); 
    fun(arg) 
    fun(arg) 

def do_four(fun, arg): 
    do_two(fun, arg) 
    do_two(fun, arg) 
+0

我感谢你的帮助,但我想通过功能做到这一点只有没有循环:) – user325923

+0

@ user325923向我们解释为什么没有循环? – xtofl

+0

因为我想先理解函数。我知道如何在循环的帮助下做到这一点,但我想尝试使用函数,并在函数的每个主体中只使用两个语句。 – user325923

2

正如user2357112说,你需要使用一个循环:

def do_four(fun, value): 
    for _ in range(4): 
     fun(value) 

编辑:没有一个循环,你可以这样做黑客:

def do_four(fun, value): 
    [fun(value), fun(value), fun(value), fun(value)] 

但是,这是一个非常丑陋的黑客。你需要学习如何使用循环,而不是如何编写臭味的代码。

+0

但有四个陈述我认为:D – user325923

+0

哈我想知道这是如何计数。一个声明有四个嵌套语句? –

0

问题要你写一个,可以这样调用函数:

do_four(say_hello, "you") 

当你试图将它写周围的其他方式。你正在学习功能 - 很好!理解函数是你可以传递给其他函数的东西,这是解决这个问题的关键,也可能是你从这个练习中采取的方法。

0

使用递归:

def do_four(fn, arg, n=4): 
    if n: 
     do_four(fn, arg, n-1) or fn(arg) 

或者用1线体

def do_four(fn, arg, n=4): 
    not n or do_four(fn, arg, n-1) or fn(arg) 
0

当你想只有两个语句然后用模量,使其:

for index in range(4): 
    if index%2 == 0: 
     print("foo") 

这给出:

foo 
foo 

所以你的情况:

def some_func(arg): 
    print(arg) 

def do_four(fun, arg): 
    for index in range(0, 4): 
     if index%2 == 0: 
      fun(arg) 

希望这可以帮助。问候CHRI

编辑 Sry基因,我没有回答这个问题(在一个函数的两个语句)

0

您可以一起使用递归使用可选参数来避免环路,但是这需要内至少3语句功能:

def do_four (func, arg, repeat=4): 
    if repeat > 0: 
     func(arg) 
     do_four(func, arg, repeat-1)