如何获得Python复合兴趣计算器以给出正确答案? [英] How to get Python Compound Interest Calculator to give the correct answer?

查看:55
本文介绍了如何获得Python复合兴趣计算器以给出正确答案?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当您输入本金,复利(每年,每月等),利率(0.03等)以及年数.

上一个问题的链接

因此,从上一个代码中,我删除了 p = 10000,n = 12,r = 0.08 ,因为当您输入不同的数字时,它将给出很大的数字.我希望它能根据输入的数字进行计算,但事实并非如此.

 #用户必须输入本金,复合利率,年利率和年份.P = int(input(请输入启动原理."))n = int(输入(输入复合的输入率.(每天,每月,每季度,半年,每年)")))r =浮动(输入(输入年利息金额.(十进制)"))t = int(input(输入年数."))最终= P *((((1 +(r/n))**(n * t))))#显示年数后的最终金额.打印(最终的数量",t,年是最终的")#目前,它显示一个非常随机的数字.请输入启动原理.1000输入复合入学率.(每日,每月,每季度,半年,每年)1输入年度利息金额.(十进制)0.01输入年数.1个1年后的最终金额为1010.0 

最终金额应为 1000.10 .不知道发生了什么.试图查看是否有一种方法可以使 P,n,r 等于用户输入的数量,从而得出正确的最终答案.

解决方案

基于此:复利公式FV = P(1 + r/n)^ Yn,其中P是初始本金,r是年利率,Y是投资年数,n是每年的复利期数.FV是终值,表示本金在Y年后增长到的金额.

  P = int(input(请输入起始原理."))n = int(input(输入每年的复利期数.")r = float(input(输入年利率.例如15表示15%"))y = int(input(输入年数."))FV = P *((((1 +((r/100.0)/n))**(n * y)))打印(之后的最终金额",y,年是",FV) 

I've run into the issues of my Compound Interest Calculator giving the wrong calculation when you input the principal, compounded interest (yearly, monthly, etc..), interest rate (0.03, etc...), and the number of years.

Previous question's link

So from the previous code, I removed p = 10000, n = 12, r = 0.08 because it would give a large number when you input different numbers. My hope was it would calculate it with the numbers inputted, but it doesn't.

# User must input principal, Compound rate, annual rate, and years.
P = int(input("Enter starting principle please. "))
n = int(input("Enter Compound intrest rate.(daily, monthly, quarterly, half-year, yearly) "))
r = float(input("Enter annual interest amount. (decimal) "))
t = int(input("Enter the amount of years. "))

final = P * (((1 + (r/n)) ** (n*t)))
#displays the final amount after number of years.
print ("The final amount after", t, "years is", final)
# At the moment it is displaying a very random number.

Enter starting principle please. 1000
Enter Compound intrest rate.(daily, monthly, quarterly, half-year, yearly) 1
Enter annual interest amount. (decimal) 0.01
Enter the amount of years. 1
The final amount after 1 years is 1010.0

The final amount should be 1000.10. Not sure what is going on. Trying to see if there is a way to make P, n, r equal the number of user inputs that will result in the correct final answer.

解决方案

Based on this: Compound Interest Formula FV = P (1 + r / n)^Yn, where P is the starting principal, r is the annual interest rate, Y is the number of years invested, and n is the number of compounding periods per year. FV is the future value, meaning the amount the principal grows to after Y years.

P = int(input("Enter starting principle please. "))
n = int(input("Enter number of compounding periods per year. "))
r = float(input("Enter annual interest rate. e.g. 15 for 15% "))
y = int(input("Enter the amount of years. "))

FV = P * (((1 + ((r/100.0)/n)) ** (n*y)))

print ("The final amount after", y, "years is", FV)

这篇关于如何获得Python复合兴趣计算器以给出正确答案?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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