【问题标题】:Sometimes code work and soemtimes it does not work? Please review code有时代码工作,有时它不工作?请检查代码
【发布时间】:2019-05-19 19:17:53
【问题描述】:
def tri_recursion(k):
  if(k>0):
    result = k+tri_recursion(k-1)
    print(result)
  else:
    result = 0
    return result


def get_factorial(k):
    if (k<2):
        result= 1
        return result
    else:
        result = k*get_factorial(k-1)
        print(result)

【问题讨论】:

  • 请编辑问题,适当格式化代码,提供minimal reproducible example 或至少说明您如何调用这些函数、它们应该做什么、您期望什么结果以及您得到什么结果.并查看help center 了解如何提问

标签: python-3.7


【解决方案1】:

修正因子程序

 def get_factorial(k):
        if k == 0:
            return 1
        else:
            return k * get_factorial(k-1)

修正的 TRI_RECURSION 程序

def tri_recursion(k):
    if k > 0:
        result = k + tri_recursion(k-1)
    else:
        result = 0
    return result

只需使用合法整数值和print()它们的返回值调用这些函数中的任何一个

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-12
    • 1970-01-01
    • 2021-02-03
    • 2022-01-12
    • 2019-11-12
    • 2014-11-19
    • 2013-06-03
    相关资源
    最近更新 更多