使递归函数返回一个Tup

2024-10-01 19:23:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我希望下面的函数返回一个每年的元组,如果它是5年,它将给我一个元组year1,year2,year3,year4,year5。在

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

Tags: 函数returndef元组salarypercentagegrowthtup
1条回答
网友
1楼 · 发布于 2024-10-01 19:23:14
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)]

相关问题 更多 >

    热门问题