I was using a for loop to pass the result of a method to the method again and complete the modification on two lists.
def loop(a, b)
#modification on a, b
return a, b
a = [1, 1, 1]
b = [2, 2, 2]
for i in np.arange(100):
a, b = loop(a, b)
In my code where I encountered this question, I defined another method in the loop method:
def loop(a, b, c): # a = cash i own; b = shares i own; c=price
buying = a[0]*0.1/c[-1] #number of shares I will spend
a.append(a[-1])
b.append(b[-1])
if c[-1]/c[-2] > 1.01, order(a, b, c)
def order(a, b, c):
a[-1] = a[-2] - buying*c[-1]
b[-1] = b[-2] + buying
return
return a, b
for i in np.arange(100):
a, b = loop(a, b, c)
I want to store the cash and shares information in a, b lists. If the buying condition is not satisfied, the new element added to the list should just be the last one. If the buying condition is satisfied then the element just added to the list will be changed. However, when I run the code, at some point the new element added a, b will not be the last one in the list but the initial one, which ruins the whole thing.