在Python中递归返回三元组似乎并不可行

问题描述 投票:0回答:1

重写一个简单的ifelse就可以了。

def factorial(x):
  return 1 if (x == 1) else factorial(x - 1) 

print(factorial(4))
python recursion ternary
1个回答
3
投票

这不是三元的问题,而是函数本身的定义问题。这应该是

def factorial(x):
  return x if x == 1 else x * factorial(x - 1)
© www.soinside.com 2019 - 2024. All rights reserved.