[英]Investment calculator refuses to calculate properly
My program runs but definitely not how it's meant to.我的程序运行但绝对不是它的本意。 No matter which input I put it, it always says "You've earned: $0.00" and I'm tried moving "months_invested = years_investment / 12" to multiple places and even adding or taking away bits.
无论我输入哪个,它总是说“你赚了:0.00 美元”,我尝试将“months_invested = years_investment / 12”移动到多个地方,甚至添加或删除位。
The investment calculator is meant to compound this stuff monthly but I cannot seem to get it right.投资计算器旨在每月计算这些东西,但我似乎无法正确计算。 One problem turns to two and it's like the hydra to me, where you fix one thing and it multiplies itself.
一个问题变成两个问题,这对我来说就像九头蛇,你解决了一件事情,它就会成倍增加。
"""
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()
I have a feeling this is what you are trying to achieve.我有一种感觉,这就是您要实现的目标。 There were some adjustments to be made which I'll explain below the code:
需要进行一些调整,我将在代码下方进行解释:
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()
Output:输出:
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
As G.Anderson stated, there's no need to initialize variables with value 0 (or any value).正如 G.Anderson 所说,没有必要用值 0(或任何值)初始化变量。 Simply define it as the input.
只需将其定义为输入即可。
The months investment formula was wrong and troubled the loop.月投资公式错误,困扰循环。 Also, if that was fixed, the loop would run infinitely since there was no limit or condition that would end it (that's what the
months_invested += -1
is for. To decrease the value of the variable each time the loop passes so it stops sometime (that is, when there are no more investments months).此外,如果这是固定的,循环将无限运行,因为没有限制或条件可以结束它(这就是
months_invested += -1
的目的。每次循环通过时减少变量的值,以便它停止某个时候(也就是说,当没有更多的投资月份时)。
Finally remember your function is printing a result and not returning anything so be careful when using it in advance.最后记住你的函数正在打印结果而不返回任何东西,所以提前使用它时要小心。
If you needed compound interest (monthly) then you should use the formula for compound interest.如果您需要复利(每月),那么您应该使用复利公式。 That is
(((1+i) ** months) -1) * $
.即
(((1+i) ** months) -1) * $
。 In this case I'm using total_money += interests
to sum the interests generated in the period to the total amount of money, which will be reinvested in the following period (hence, compound interest).在这种情况下,我使用
total_money += interests
将期间产生的利息加到资金总额中,这些资金将在下一期间再投资(因此,复利)。
There is no real need to use a while loop, other approaches might be more efficient, using lists or arrays for instance.没有真正需要使用 while 循环,其他方法可能更有效,例如使用列表或数组。 However I respected the original structure of your code for my answer.
但是,对于我的回答,我尊重您代码的原始结构。
months_invested
is set by the line months_invested = float()
, where it is initialized to zero. months_invested
由行设置months_invested = float()
,它被初始化为零。 This means that the while loop never runs.这意味着 while 循环永远不会运行。 You want
months_invested = years_investment * 12
.你想要
months_invested = years_investment * 12
。
It's worth noting that this still won't do what you want, but I don't want to provide you with an entire answer: struggling with problems is an important part of learning.值得注意的是,这仍然不会如你所愿,但我不想为你提供完整的答案:与问题作斗争是学习的重要组成部分。
You've got a couple things going here.你在这里有几件事。
float()
before hitting the while loop. float()
改变。years_investment = 0
is not in the main()
function.years_investment = 0
之后的所有内容都不在main()
函数中。 (this is probably just a copy/paste issue when posting the question.) months_investment
value.months_investment
值。 I did some refactoring to get you where I think you want to be.我做了一些重构,让你到达我认为你想去的地方。
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.