def factorial(x): m=x-1 while m>0: t=x*m return t m-=1 else: return 1
Much simpler to do this:
def factorial1(n): if n < 2: return 1 return n * factorial(n-1)
You see, factorial 0 and 1 both equal to 1, so this line takes care of that:
if n < 2:
Then any other factorial is take care of by doing this:
return n * factorial(n-1)
Hope that helps!
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.