如何让Python复合利息计算器给出正确的答案

问题描述 投票:0回答:4

之前发过一个关于错误的问题。感谢这里的一些人,我能够解决这个问题。现在我遇到了复合利息计算器的问题,当您输入本金,复利(年度,月度等),利率(0.03等等)和金额时,给出错误的计算。年份。其他Q链接:final = P * (((1 + (r/n)) ** (n*t))) TypeError: unsupported operand type(s) for /: 'str' and 'int'所以从前面的代码我删除了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

最终金额应为1000.10。不确定发生了什么。试图看看是否有办法使P,n,r等于用户输入的数字,这将导致正确的最终答案。

提前致谢。

python calculator
4个回答
1
投票

如果您对百分比感兴趣,则应在代码中注意这一点。

final = P * (((1 + (r/(100.0 * n))) ** (n*t)))

1
投票

基于此:复利计算公式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)

1
投票

请尝试下面的复合兴趣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)

0
投票
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)
© www.soinside.com 2019 - 2024. All rights reserved.