Reputation: 11
I have this python function where I am creating an annuity whose value doubles every 2nd year. I have attempted two approaches, the first is recursive, however I get an error 'maximum recursion depth exceeded in comparison' or that the type is None (when I run the function as annu(10000, 40,0.1045)
def annu(MNY, n, i):
k=MNY/i
c=((1+i)**(n-1))-1
FV=k*c
FV1=0
FV2=0
for y in range(1, n):
if (y%2)==1:
FV1=annu(MNY,(n),i)
print('FV1 is', FV1)
return round(FV1,3)
else:
FV2=2*FV1
print('FV2 is', FV2)
return round(FV2,2)
FV=FV1+FV2
inv_bal=FV
return round(inv_bal,1)
The second involves no recursion, however this gives me an incorrect answer:
def inp(PMT, n,i):
balance=0
FV1=0
FV2=0
k=PMT/i
c=((1+i)**n)-1
FV=k*c
for j in range(0,n):
if j%2==1:
FV=2*((FV)*(1+i)**(n-1))
interest=balance*i
balance=balance+interest + PMT
FV1=FV1+balance
print('FV1 is', FV1)
else:
FV2=2*((FV1)*(1+i)**(n-1))
interest=balance*i
balance=balance+interest + PMT
FV2=FV2+balance
print('FV2 is', FV2)
FV=FV1+FV2
print('inv_bal', FV)
inv_bal=FV
I am expecting an answer of around 4*10**9 if I run it as annu(10000,40,0.1045)
Upvotes: 1
Views: 212