简体   繁体   中英

How can I increase a value by 5% monthly using a while loop?

I'm attempting to take a starting balance and increase the value by 5% each month. I then want to feed this new balance back into the equation for the next month. I've attempted to do this using a while loop but it doesn't seem to be feeding the new balance back in.

I'm using 60 months (5 years) for the equation but this can be altered

counter = 1
balance = 1000
balance_interest = balance * .05

while counter <= 60:
    new_monthly_balance = (balance + balance_interest)*(counter/counter)
    print(new_monthly_balance)
    balance = new_monthly_balance
    counter += 1

You never change balance_interest in the loop.

What do you intend to do with *(counter/counter) ? This merely multiplies by 1.0, which is a no-op.

while counter <= 60:
    balance *= 1.05
    print(balance)
    counter += 1

Better yet, since you know how many times you want to iterate, use a for :

for month in range(60):
    balance *= 1.05
    print(balance)

BTW, just what sort of finance has a constant 5% monthly increase???

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM