我对编程非常陌生,我的任务是编写一个简单的函数(而不是只打印每个月的公式),该函数接受存款和月数,并在6个月后返回复利,每个月都有相同金额的新存款
我们将非常感谢您向正确方向提供的任何帮助或推动
Initial and monthly deposit is $100
Monthly interest rate is 0.00417 (0.05 annually)
出于我自己的目的,我写下了以下公式:
# month1 = 100 * (1.0 + 0.00417)
# month2 = 100 + month1 * (1.0 + 0.00417)
# month3 = 100 + month2 * (1.0 + 0.00417)
# month4 = 100 + month3 * (1.0 + 0.00417)
# month5 = 100 + month4 * (1.0 + 0.00417)
# month6 = 100 + month5 * (1.0 + 0.00417)
并试图把它变成一个程序:
def compound_interest(deposit, n):
# n is number of months
first_month = deposit * (1.0 + 0.00417)
next_month = deposit + first_month * (1.0 + 0.00417)
for i in range(n):
first_month += next_month
return i * deposit + first_month
print(compound_interest(100, 6))
不用说,我认为我在这里找错了方向——我无法将数学与代码相协调
试试这个:
相关问题 更多 >
编程相关推荐