This code is supposed to Iterate a (decreasing) list of numbers and stop right before they go below zero.
I am also trying to print the number of iterations needed to get there.
This code not only it prints out one negative number, but the number of iterations printed are always 22, even though the code iterates less times (see value of n) and print(L) always prints the value correspondent
to n=6, no matter which value I choose to give n.
I am also trying to print the number of iterations needed to get there.
This code not only it prints out one negative number, but the number of iterations printed are always 22, even though the code iterates less times (see value of n) and print(L) always prints the value correspondent
to n=6, no matter which value I choose to give n.
def f(balance,r,A,n): r=5.0 A=1000.0 n=18 balance=10000 L=[] for year in range(n): balance=(balance)*(r/100.0 +1)- A L.append(balance) if balance <=0: balance=0 break return L print(f(balance,r,A,n)) print(L,n) #should print the last non negative value and the iterations #needed to get there