2012-12-07 135 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/