我知道已经有人问过这个问题,但是我在执行该问题时遇到了麻烦,所以我在这里问。
到目前为止,我的python代码是:
def calculate():
p = 10000 # dollars
n = 12 # months
r = 8 # interest %
t = float(raw_input("Type the number of year that the money will be compounded for:"))
b = p * r
a = b ** t
print(a)
calculate()
答案 0 :(得分:1)
就像约翰·科尔曼在评论中提到的那样,您根本没有实现该公式。
在您的代码中,您仅将p
乘以r
和b
到t
的幂。
正确的公式如下:p*(1+(r/n))**(n*t)
。
我建议您阅读与python基本运算符相关的文章,您可以在Python Course上找到它。
def calculate():
p = 10000
n = 12
r = .08
t = float(input("Type the number of year that the money will be compounded for:"))
formula = p*(1+(r/n))**(n*t)
return formula
print (calculate())
答案 1 :(得分:0)
因此,由于您正在编写函数,因此需要返回一个值。我会说直接将所有值输入到函数中,就像这样:
def calculate(P, r, n , t):
exponent = n*t
parends = 1 + (r/n)
val = parends ** exponent
ans = P * val
return ans
print(calculate(10000, .08, 12, 1))
我将检查其他资源以学习如何使用功能。 Codeacademy是一个很好的学院。
这里的功能只是不成碎片:
def shorter(P, r, n, t):
return P*(1+(r/n))**(n*t)
print(shorter(10000, .08, 12, 1))