The following is a recursive function designed to return the factorial of a number.
def factorial(number, target): if number == 1: return target else: target = target*number factorial((number-1), target) print factorial(7, 1)
As you'll see if you run it however it doesn't return my 'target' variable (the factorial of 'number') but instead returns 'None'. I can get it to produce the factorial by changing line 3 from 'return target' to 'print target', but this is an imperfect solution because it doesn't allow me to use the output of this function in subsequent code. So, how do I get the function to 'return' the target?
While I'm at it, I'd also appreciate some help on this next bit of code which is designed to count down from 12 to 3.
def reduce(n): while n != 3: print n reduce((n-1)) reduce(12)
When I run it, it gets down to the number 4 and then ends up in an infinite loop, endlessly repeating '4'. Can anyone explain why?