不知道什么是错的,代码是给12,而不是24阶乘代码不工作
def factorial(x):
m=x-1
while m>0:
t=x*m
m-=1
return t
else:
return 1
print factorial(4)
不知道什么是错的,代码是给12,而不是24阶乘代码不工作
def factorial(x):
m=x-1
while m>0:
t=x*m
m-=1
return t
else:
return 1
print factorial(4)
你的代码返回值和分配新的价值为t每次迭代
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
因为你正在返回while循环的第一次迭代。 –
这不是唯一的问题,'t = x * m'没有意义。 – polku