Question

I am a beginner programmer and I am working on an assignment that requires me to do nested loops as part of a finance operation. I have written most of the code and the numbers work according (such as interest and such), however the issue arises when I try print out the savings summary for the years given.

import math

def main():

    #This will be hardcoded values for the years running, savings amount and annual interest             and calculate the monthly interest rate

    savingsAmount = 500
    annualInterest = 0.12
    yearsRunning = 2

    monthlyInterest = annualInterest / 12

    #This will state the accumulator variables for totals of investment balance (month), savings (YTD), and interest earned (YTD)

    totalInvestBal = 0
    totalSavings = 500
    totalInterest = 0

    #This will begin the accumulator loop process

    for i in range (1, yearsRunning + 1):
        print "Savings Schedule for Year", i,":"
        print "Month    Interest    Amount  Balance"
        for i in range (1, 13):
            totalInterest = monthlyInterest * totalInvestBal
            totalInvestBal = totalSavings + totalInterest + totalInvestBal
            totalSavings = totalSavings
            print i, round(totalInterest,2), round(totalSavings,2), round(totalInvestBal,2)         
        print 
        #i becomes 12 here so we need another answer.
        print "Savings summary for year", (need a new way of saying the year here),":"
        print "Total amount saved:", totalSavings
        print "Total interest earned:", totalInterest
        print "End of year balance:", totalInvestBal


main()

Since the "i" loop index variable is updated to 12, I can place that as the year. I am working from year 1 up and I need the savings summary to be from year 1 and up as well. How would that be fixed?

Was it helpful?

Solution

Simply change the second loop variable form i to anything else (e.g. k):

for i in range (1, yearsRunning + 1):
    print
    print "Savings Schedule for Year", i,":"
    print "Month    Interest    Amount  Balance"
    for k in range (1, 13):
        totalInterest = monthlyInterest * totalInvestBal
        totalInvestBal = totalSavings + totalInterest + totalInvestBal
        totalSavings = totalSavings
        print k, round(totalInterest,2), round(totalSavings,2), round(totalInvestBal,2)         
    print 
    #IF WE KEEP ONLY i IT becomes 12 here so we need another -> VARIABLE!!!!! for example K!!.
    print "Savings summary for year %s:" %(i) #use this " words words %s words" %(variable name)
    print "Total amount saved:", totalSavings
    print "Total interest earned:", totalInterest
    print "End of year balance:", totalInvestBal

OTHER TIPS

For starters, I added some \t 's to your code- they denote printing a tab for your output so that the output lines up better.

def main():

    #This will be hardcoded values for the years running, savings amount and annual interest             and calculate the monthly interest rate

    savingsAmount = 500
    annualInterest = 0.12
    yearsRunning = 2

    monthlyInterest = annualInterest / 12

    #This will state the accumulator variables for totals of investment balance (month), savings (YTD), and interest earned (YTD)

    totalInvestBal = 0
    totalSavings = 500
    totalInterest = 0

    #This will begin the accumulator loop process

    for i in range (1, yearsRunning + 1):
        print
        print "Savings Schedule for Year", i,":"
        print "Month \tInterest \tAmount \tBalance"
        for i in range (1, 13):
            totalInterest = monthlyInterest * totalInvestBal
            totalInvestBal = totalSavings + totalInterest + totalInvestBal
            totalSavings = totalSavings
            print i, "\t", round(totalInterest,2), "\t\t",  round(totalSavings,2), "\t", round(totalInvestBal,2)         
        print 
        #i becomes 12 here so we need another answer.
        print "Savings summary for year" #, (need a new way of saying the year here),":"
        print "Total amount saved:", totalSavings
        print "Total interest earned:", totalInterest
        print "End of year balance:", totalInvestBal


main()

Since this is an assignment, I'll point out some errors I see and let you try to fix them. If you look at the output:

Savings Schedule for Year 1 :
Month   Interest    Amount  Balance
1   0.0         500.0   500.0
2   5.0         500.0   1005.0
3   10.05       500.0   1515.05
4   15.15       500.0   2030.2
5   20.3        500.0   2550.5
6   25.51       500.0   3076.01
7   30.76       500.0   3606.77
8   36.07       500.0   4142.84
9   41.43       500.0   4684.26
10  46.84       500.0   5231.11
11  52.31       500.0   5783.42
12  57.83       500.0   6341.25

Savings summary for year
Total amount saved: 500
Total interest earned: 57.8341733327
End of year balance: 6341.2515066

your balance isn't correct, look at this line of code for a solution:

totalInvestBal = totalSavings + totalInterest + totalInvestBal

For your question, the way I'm interpreting it is that you want to have a separate interest-earned value in just the timeframe of a year, after the first year. So the output that I think you want for year 2 is (assuming you fix the end of year balances):

Savings Schedule for Year 2 :
Month   Interest    Amount  Balance
1   63.41       500.0   6904.66
2   69.05       500.0   7473.71
3   74.74       500.0   8048.45
4   80.48       500.0   8628.93
5   86.29       500.0   9215.22
6   92.15       500.0   9807.37
7   98.07       500.0   10405.45
8   104.05      500.0   11009.5
9   110.1       500.0   11619.6
10  116.2       500.0   12235.79
11  122.36      500.0   12858.15
12  128.58      500.0   13486.73

Savings summary for year
Total amount saved: 500
Total interest earned: 70.74733
End of year balance: 13486.7324266

Is that correct?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top