简体   繁体   English

多次调用函数并传递参数

[英]Calling function and passing arguments multiple times

I want to call the function multiple time and use it's returned argument everytime when it's called. 我想多次调用该函数,并在每次调用时使用它的返回参数。 For example: 例如:

  def myfunction(first, second, third):
         return (first+1,second+1,third+1)

1st call: myfunction(1,2,3) 2nd call is going to be pass returned variables: myfunction(2,3,4) and loop it until defined times. 第一个调用: myfunction(1,2,3)第二个调用将传递返回的变量: myfunction(2,3,4)并循环直到定义的时间。 How can I do such loop? 我该如何做循环? Thank you! 谢谢!

a,b,c = 1,2,3
while i<n:
    a,b,c = myfunction(a,b,c)
    i +=1
def myF(x,y,z,i):
    print x, i
    while i:
        x += 1
        i -= 1
        return myF(x,i)

This will keep calling myF until i is 0 which will break the while loop, example: 这将一直调用myF直到i0 ,这将中断while循环,例如:

>>> myF(1,10)
1 10
2 9
3 8
4 7
5 6
6 5
7 4
8 3
9 2
10 1
11 0

For three arguments, you can do as follows: 对于三个参数,您可以执行以下操作:

>>> def myF(x,y,z,i):
        print x,y,z,i
        while i:
            i -= 1
            x,y,z = map(lambda s:s+1,(x,y,z))
            return myF(x,y,z,i)


>>> 
>>> 
>>> myF(1,1,1,10)
1 1 1 10
2 2 2 9
3 3 3 8
4 4 4 7
5 5 5 6
6 6 6 5
7 7 7 4
8 8 8 3
9 9 9 2
10 10 10 1
11 11 11 0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM