初级Python代码问题:累加器循环

2024-05-29 02:37:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我是一个初学者程序员,我正在做一项任务,要求我做嵌套循环作为财务操作的一部分。我已经写了大部分代码和数字的工作原理(如利息等),但是当我打印出给定年份的储蓄总结时,问题就出现了。在

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()

因为“i”循环索引变量更新为12,所以我可以将其作为年份。我从第一年开始工作,我需要储蓄总结从第一年和更高。怎么解决?在


Tags: andoftheforthisyearwillprint
2条回答

只需将第二个循环变量形式i更改为任何其他形式(例如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

对于初学者,我在代码中添加了一些\t-它们表示为输出打印一个制表符,以便更好地排列输出。在

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()

既然这是一项作业,我会指出我看到的一些错误,让你去纠正它们。如果您查看输出:

^{pr2}$

您的余额不正确,请查看以下代码行以获得解决方案:

totalInvestBal = totalSavings + totalInterest + totalInvestBal

对于你的问题,我的解释是,你想在一年内,在第一年之后,有一个单独的利息收入值。因此,我认为第二年你想要的产出是(假设你修正了年终余额):

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

对吗?在

相关问题 更多 >

    热门问题