使递归函数返回元组

时间:2015-10-24 22:41:21

标签: python function recursion tuples

我希望以下函数每年返回一个元组,即。如果它的5年它会给我一年,一年,三年,四年,五年的元组。

def nextSalaryFixed(salary, percentage, growth, years):
if years == 1:
        tup = (salary * (percentage * 0.01), )
        return tup[years-1]
    else:
        tup = (nextEggFixed(salary, percentage, growth, years - 1) * ((1 + (0.01 * growth))) + (salary * (percentage * 0.01)))
        print(tup)
        return tup

1 个答案:

答案 0 :(得分:0)

result = []

def nextSalaryFixed(salary, percentage, growth, years):
    if years == 1:
        tup = salary * (percentage * 0.01)
    else:
        tup = (nextSalaryFixed(salary, percentage, growth, years - 1) *
            ((1 + (0.01 * growth))) + (salary * (percentage * 0.01)))

    result.append((years, tup))
    return tup

nextSalaryFixed(10000, 10, 5, 5)
result # [(1, 1000.0), (2, 2050.0), (3, 3152.5), (4, 4310.125), (5, 5525.63125)]