繁体   English   中英

投资计算器拒绝正确计算

[英]Investment calculator refuses to calculate properly

我的程序运行但绝对不是它的本意。 无论我输入哪个,它总是说“你赚了:0.00 美元”,我尝试将“months_invested = years_investment / 12”移动到多个地方,甚至添加或删除位。

投资计算器旨在每月计算这些东西,但我似乎无法正确计算。 一个问题变成两个问题,这对我来说就像九头蛇,你解决了一件事情,它就会成倍增加。

"""

 InvestmentCalculator.py helps generate how much interest one earns after a certain period of time
 """


 def main():
     total_money = 0
     months_invested = 0
     years_investment = 0

 investment = float(input("How much would you like to invest? "))
 years_investment = float(input("How many years would you like to invest? "))
 interest_rate = float(input("What is the interest rate? "))
 total_money = float()
 months_invested = float()


 while months_invested > 0:
     months_invested = (years_investment / 12) - 1
     total_money = investment +  total_money
     print("You have earned ${:,.2f}".format(total_money))
 else: print("You've earned a total of ${:,.2f}".format(total_money))


 main()

我有一种感觉,这就是您要实现的目标。 需要进行一些调整,我将在代码下方进行解释:

def main():    
    total_money = float(input("How much would you like to invest? "))
    years_investment = float(input("How many years would you like to invest? "))
    interest_rate = float(input("What is the monthly interest rate in %? "))

    months_invested = years_investment * 12
    while months_invested > 0:
        interests = interest_rate / 100 * total_money
        total_money += interests
        print("You have earned ${:,.2f}".format(total_money))
        months_invested += -1

main()

输出:

You have earned $101.00
You have earned $102.01
You have earned $103.03
You have earned $104.06
You have earned $105.10
You have earned $106.15
You have earned $107.21
You have earned $108.29
You have earned $109.37
You have earned $110.46
You have earned $111.57
You have earned $112.68

正如 G.Anderson 所说,没有必要用值 0(或任何值)初始化变量。 只需将其定义为输入即可。

月投资公式错误,困扰循环。 此外,如果这是固定的,循环将无限运行,因为没有限制或条件可以结束它(这就是months_invested += -1的目的。每次循环通过时减少变量的值,以便它停止某个时候(也就是说,当没有更多的投资月份时)。

最后记住你的函数正在打印结果而不返回任何东西,所以提前使用它时要小心。

如果您需要复利(每月),那么您应该使用复利公式。 (((1+i) ** months) -1) * $ 在这种情况下,我使用total_money += interests将期间产生的利息加到资金总额中,这些资金将在下一期间再投资(因此,复利)。

没有真正需要使用 while 循环,其他方法可能更有效,例如使用列表或数组。 但是,对于我的回答,我尊重您代码的原始结构。

months_invested由行设置months_invested = float() ,它被初始化为零。 这意味着 while 循环永远不会运行。 你想要months_invested = years_investment * 12

值得注意的是,这仍然不会如你所愿,但我不想为你提供完整的答案:与问题作斗争是学习的重要组成部分。

你在这里有几件事。

  1. while 循环将在满足条件时进入,并且似乎永远不会在命中 while 循环之前从float()改变。
  2. 缩进关闭, years_investment = 0之后的所有内容都不在main()函数中。 (这可能只是发布问题时的复制/粘贴问题。)
  3. 当你进入 for 循环时,你最终会坚持下去,因为你实际上并没有减少months_investment值。

我做了一些重构,让你到达我认为你想去的地方。

def main():
    investment = float(input("How much would you like to invest? "))
    months_investment = float(int(input("How many years would you like to invest? ")) * 12)
    interest_rate = float(input("What is the interest rate? "))
    total_money = 0

    while months_investment > 0:
        total_money += investment * interest_rate
        print("You have earned ${:,.2f}".format(total_money))
        months_investment -= 1
    else: print("You've earned a total of ${:,.2f}".format(total_money))


main()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM