【问题标题】:Make Recursive Function Return a Tuple使递归函数返回一个元组
【发布时间】:2016-01-24 06:37:46
【问题描述】:

我希望以下函数每年返回一个元组,即。如果它是 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

【问题讨论】:

  • nextEggFixed?那是什么定义的?

标签: python function recursion tuples


【解决方案1】:
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)]

【讨论】:

    猜你喜欢
    • 2013-08-07
    • 2022-01-02
    • 1970-01-01
    • 2022-01-26
    • 2016-01-25
    • 2014-01-22
    • 1970-01-01
    • 2015-10-23
    • 2015-04-27
    相关资源
    最近更新 更多