私は初心者のプログラマーで、金融業務の一環としてネストされたループを実行する必要がある課題に取り組んでいます。私はほとんどのコードを書いており、数値はそれに応じて機能します (利息など) が、指定された年の貯蓄の概要を印刷しようとすると問題が発生します。
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 に更新されるので、それを年として配置できます。私は 1 年目から働いており、1 年目以降の貯蓄の概要も必要です。それはどのように修正されますか?