Home > Mobile >  How can use the result of a function call in the same function again with a loop?
How can use the result of a function call in the same function again with a loop?

Time:04-01

def next1(x,y):
    ymap = x y
    xmap = x-y
    return np.array([xmap,ymap])

A = next1(x,y)
B = next1(A[0],A[1])
C = next1(B[0],B[1])
D = next1(C[0],B[1])
E = next1(D[0],D[1])
F = next1(E[0],E[1])

I want to use the result obtained from the next1 function as an input argument further and so on for 100 times. I am not able to understand how to do it using loop.

CodePudding user response:

Since your function returns two values, you can "unpack" these to overwrite the original variables at every iteration:

x, y = 1, 1 #initial values
for i in range(100):
    x, y = next1(x, y)

CodePudding user response:

A variation you might find interesting:

xy = x,y
for i in range(100):
    xy = next1(*xy)
  • Related