2016-04-15 61 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)