0

我正在尝试在 Python 中构建一个递归函数来查找复利。到目前为止,这是我的代码。

def compound_interest(principal, rate, years):
    return principal*(1 + rate)**years

def compound_interest_recursive(principal, rate, years)
    if years == 0:
        return principle
    else:
        return 



principal_str = input("Enter the principal ")
principal = int(principal_str)

rate_float = input("Enter the interest rate ")
rate = float(rate_float)

years_str = input("Enter the number of years ")
years = int(years_str)

print("Principal after", years,"year(s) is",compound_interest\
      (principal, rate, years))

有人可以告诉我我错过了什么吗?谢谢。我试图让它输入数字然后打印出来。

4

2 回答 2

6
于 2013-07-24T16:56:36.720 回答
-2
def compound_interest(prin,rate,year):
    if year<=0:

        return prin
    else:
        return compound_interest(prin+prin*rate/100,rate,year-1)

 compound_interest(100000,12,3)
 140492.8
于 2013-07-24T17:27:33.670 回答