我试图弄清楚多年来如何获得兴趣和本金以正确显示。这是我遇到麻烦的部分代码:

print ('Luke\n-----')
print ('Year\tPrincipal\tInterest\t Total')

LU_RATE = .05
YEAR = 1
Principal = 100

for YEAR in range (1,28):

   # Calculating Luke's total using formula for compounding interest
   Lu_Total = (Principal * ((1 + LU_RATE) ** YEAR))

   # I realize it's a logical error occurring somewhere here
   Lu_Interest = #I'm not sure what to code here
   Lu_Principal = #And here

   # Displaying the Principal, Interest, and Total over the 27
   print (YEAR,'\t%.02f\t\t %.02f\t\t %.02f' %(Lu_Principal, Lu_Interest, Lu_Total))


这是显示的内容(当然要减去注释符号):

Luke
-----
Year    Principal    Interest    Total
1        #            #           105.00
2        #            #           110.25
3        #            #           115.76
4        #            #           121.55
5        #            #           127.63
6        #            #           134.01
#etc etc....


我尝试编写的每个等式都具有第一年的正确利息,但最终却将本金作为总计。每年过去都会计算出错误的数字。

它应该看起来像:

Luke
-----
Year    Principal    Interest    Total
1        100.00       5.00       105.00
2        105.00       5.25       110.25
3        110.25       5.51       115.76
#etc etc....


我整天都在研究和开发它,只是似乎无法弄清楚。预先感谢您的任何帮助或建议。

最佳答案

这是我所做的:

print ('Luke\n-----')
print ('Year\tPrincipal\tInterest\t Total')

LU_RATE = .05
YEAR = 1
Principal = 100
Prev_Principal = 100   #added to store previous year principal

for YEAR in range (1,28):

    # Calculating Luke's total using formula for compounding interest
    Lu_Total = (Principal * ((1 + LU_RATE) ** YEAR))

    Lu_Interest = Lu_Total - Prev_Principal

    Lu_Principal = Lu_Total - Lu_Interest

    Prev_Principal = Lu_Total


    # Displaying the Principal, Interest, and Total over the 27
    print (YEAR,'\t%.02f\t\t %.02f\t\t %.02f' %(Lu_Principal, Lu_Interest, Lu_Total))

关于python - python复利,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35496635/

10-12 21:46