cb vaswar
cb vaswar

Reputation: 7

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

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.

Upvotes: 0

Views: 210

Answers (2)

Scott Hunter
Scott Hunter

Reputation: 49803

A variation you might find interesting:

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

Upvotes: 0

not_speshal
not_speshal

Reputation: 23146

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)

Upvotes: 2

Related Questions