我是如何通过每月捐款建立复利的计划 [英] How did I build a program for compound interest with monthly contributions

查看:76
本文介绍了我是如何通过每月捐款建立复利的计划的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想开发一个复合兴趣的程序,允许每月在Python中贡献。语法除外 - 需要帮助配方。



我尝试过:



I want to develop a program for compound interest allowing monthly contributions in Python. Syntax aside- need help with formula.

What I have tried:

Init_Amt = input("Enter initial amount")
Interest_rate = input("Enter the interst rate")
Num_years = input("Number of years")
Monthly_Amount = input("Monthly Amount")

Init_Amt = float(Init_Amt)
Interest_rate = float(Interest_rate)
Num_years = int(Num_years)
Monthly_Amount = float(Monthly_Amount)

PV = Init_Amt
IR = Interest_rate
N = Num_years


N_m = N*12 #in terms of months
M = Monthly_Amount



def compound(PV, N, M, IR):
    for i in range(N):
        PV += (M * 12)
        FV = (PV*(1+IR)**N)
        print(i + FV)




if __name__ == "__main__":
    compound(PV, N, M, IR)

推荐答案

您永远不会说出遇到的问题。除了公式,我只会评论你的代码,这是本论坛的目的。建议你重新访问Python的基本语法,这是代码块的缩进,你没有注意到这一点:

You never state the problem you have encountered. Formula aside, I will only comment on your code which is the purpose of this forum. Suggest you revisit the basic syntax of Python which is the indentation of code block, you failed to observe this in:
def compound(PV, N, M, IR):






and

if __name__ == "__main__":



阅读词法分析 - Python 3.5.2文档 [ ^ ]并自己弄明白。



+++++ [第2轮] +++++

我看到你已经添加了缩进,但你仍然错过了重点,我将为你提供回答如下:


Read Lexical analysis — Python 3.5.2 documentation[^] and figure it out yourself.

+++++[Round 2]+++++
I saw that you have added the indentation, but you still miss the point, I will provide you the answer below:

def compound(PV, N, M, IR):
    for i in range(N):
        PV += (M * 12)
        FV = (PV*(1+IR)**N)
        print(i + FV)

if __name__ == "__main__":
    compound(PV, N, M, IR)



缩进是Python的分组方式代码作为块,必须与其他编程语言中使用的{}一样。


The indentation is Python' way of grouping related code as a block, must like the {} used in other programming languages.


这篇关于我是如何通过每月捐款建立复利的计划的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆