如何让 Python 复利计算器给出正确答案
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34952669/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get Python Compound Interest Calculator to give the correct answer
提问by Joshua Gremo
Had posted a question earlier about errors. Thanks to a few people here I was able to fix that issue. Now 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 amount of years. Other Q link: final = P * (((1 + (r/n)) ** (n*t))) TypeError: unsupported operand type(s) for /: 'str' and 'int'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.
之前发布了一个关于错误的问题。感谢这里的几个人,我能够解决这个问题。现在我遇到了我的复利计算器的问题,当你输入本金、复利(每年、每月等)、利率(0.03 等)和年。其他 Q 链接:final = P * (((1 + (r/n)) ** (n*t))) TypeError: unsupported operand type(s) for /: 'str' and 'int'So from the previous代码我删除了 p = 10000, n = 12, r = 0.08 因为当你输入不同的数字时它会给出一个很大的数字。我希望它会用输入的数字来计算它,但事实并非如此。
# 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 a user inputs that will result in the correct final answer.
最终金额应为 1000.10。不知道发生了什么。试图看看是否有办法使 P、n、r 等于用户输入的数字,从而得出正确的最终答案。
Thanks in advance.
提前致谢。
回答by Sagar Waghmode
If you are entering interest in percentages, you should take care of that in your code.
如果您按百分比输入利息,则应在代码中注意这一点。
final = P * (((1 + (r/(100.0 * n))) ** (n*t)))
回答by Tom Klise
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.
基于此:复利公式FV = P(1 + r / n)^Yn,其中P为起始本金,r为年利率,Y为投资年数,n为复利期数每年。FV 是未来值,意味着本金在 Y 年后增长到的金额。
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)
回答by Srihari Rao
Please try the below Python code for Compound Interest:
请尝试使用以下 Python 代码进行复利:
p = float(input('Please enter principal amount:'))
t = float(input('Please enter number of years:'))
r = float(input('Please enter rate of interest:'))
n = float(input('Please enter number of times the interest is compounded in a year:'))
a = p*(1+(r/(100*n))**(n*t))
print('Amount compounded to: ', a)
回答by LeqitShxdow
P = int(input("Enter starting principle please: "))
n = int(input("Enter number of compounding periods per year: "))
r = float(input("Enter annual interest rate: "))
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)
回答by Sudarshan G
# name
NAME=raw_input("Enter Name= ")
# Principle amount
P =float(input("Enter Principle Amount: "))
# Rate of interest
R = float(input("Enter rate of Interest: "))
#No Of years
T = float(input("Enter No of Years= "))
#compound interest calculation
CI = P * ( 1 + R / 100)**T
print"compound interest is {:.2f}".format(CI)