如何找到有关我的代码的信用卡余额?

时间:2016-03-07 03:10:24

标签: python

def balance (p, apr, mo):
mpr = 0.01*apr/12
for month in range(int(mo)):
    p= p+p*mpr
    return p

我是一个初学者,试图创建一个功能,它将在信用卡上恢复余额,从月平均值和利率开始。当我运行我的代码时,似乎循环不起作用。

1 个答案:

答案 0 :(得分:1)

 def balance (p, apr, mo):
    mpr = 0.01*apr/12.0
    for month in range(int(mo)):
        p= p+p*mpr
    return p


for i in range(1,13):
    print balance(1000,10,i)

严格来说这个mpr是不正确的(每月复合是一个简化,在幻灯片规则之前是合理的);月率应该是年率的第12个根;看到连续复合https://en.wikipedia.org/wiki/Compound_interest

def balance2 (p, apr, mo):
    mpr =  ((1+apr/100.0)**(1/12.0)-1)
    for month in range(int(mo)):
        p= p+p*mpr
    return p

print 
for i in range(1,13):
    print balance2(1000,10,i) 

输出:

1008.33333333
1016.73611111
1025.20891204
1033.75231964
1042.3669223
1051.05331332
1059.81209093
1068.64385836
1077.54922384
1086.52880071
1095.58320738
1104.71306744

1007.97414043
1016.01186777
1024.11368908
1032.28011546
1040.51166206
1048.80884817
1057.17219721
1065.60223677
1074.09949864
1082.66451888
1091.29783779
1100.0