不知道错误,代码是12而不是24
def factorial(x):
m=x-1
while m>0:
t=x*m
m-=1
return t
else:
return 1
print factorial(4)
答案 0 :(得分:1)
您的代码在第一次迭代时返回值,并为每次迭代分配新值
def factorial(x):
... t = 1
... while x>0:
... t *= x
... x-=1
...
... return t
print factorial(4)
output:
24
---- ----或
from operator import mul
def factorial(x):
return reduce(mul, range(1,x+1))
print factorial(4)
output:
24