Home > OS >  For from a range(0, value goal)
For from a range(0, value goal)

Time:01-26

'Hi, I'm trying to calculate how many months to achieve a million dollars with compounding interest and monthly investments. There are my tries.'

'This first code work, but I want to replace the 92 in the rage with a compare formula like fv >= 1000000.'

'When I place the range like here, it doesn't work.'

CodePudding user response:

Try while-loop may help:

pv = 130000  # present value
i = 4000  # regular monthly investment
r = 0.1375  # annual interest rate
n = 0  # number of months
# for n in range(0, 92):
fv = 0
while fv < 1000000:
    fv = pv * (1   r / 12) ** n   i * (((1   r / 12) ** n - 1) / (r / 12))
    n  = 1 #don't forget
    print(fv)
    print(n)

You need to manually increase the value of n

  • Related