2012-12-07 128 views
0

說我有一個功能的Python:函數參數和返回值

def equals_to(x,y): 
a + b = c 
def some_function(something): 
for i in something: 
... 

有沒有辦法使用,是由equals_to計算爲參數some_function這樣

equals_to(1,2) 
some_function(c) 
+0

什麼是'c','a'和'b'?他們從哪裏來? – alexvassel

+0

我很困惑你想要完成什麼。 'equals_to'不能編譯,它看起來像是返回一個整數,這在'for x in blah'語句中不起作用。 – Dunes

+0

'def equals_to(x,y)'應該說'def equals_to(a,b)'在輸入例子時我犯了一個錯誤。 「a + b = c」應該是'c = a + b' – Keenan

回答

3

您需要c方式到return從函數c的值。

def equals_to(x,y): 
    c = x + y   # c = x + y not a + b = c 
    return c   # return the value of c 

def some_function(something): 
    for i in something: 
    ... 
    return 

sum = equals_to(1,2)  # set sum to the return value from the function 
some_function(sum)  # pass sum to some_function 

同樣的equals_to的函數簽名採用的參數x,y但在功能使用a,b和你的任務是南轅北轍,c需要的x + y值不等於a + bc

強烈推薦:http://docs.python.org/2/tutorial/