【发布时间】:2015-04-19 07:51:12
【问题描述】:
我试图通过将其创建为 for 或 while 循环来转换此函数,但遇到了麻烦。
def fact(n):
if n<= 1:
return 1
else:
return (n)*(fact(n-1))
这是我尝试的:
def fact(n):
while n <= 1:
return 1
else:
return (n)*(fact(n-1))
【问题讨论】:
-
请参阅what is wrong with my factorial code in python,了解在 Python 中计算阶乘的各种方法。该问题的My answer 提供了各种方法的时间统计信息。