On 07/11/2010 06:28 PM, Nick Raptis wrote:

def recursfac(x,carryover=1):
    print 'x:',x,'carryover:', carryover
    if x > 1:
        carryover *= x
        carryover = recursfac(x-1, carryover)
    return carryover

And this returns
x: 3 carryover: 1
x: 2 carryover: 3
x: 1 carryover: 6
6

Done!


Also, I realized that my final code may be tough to decipher now.. A nicer way to write it would be (the functionality is still exactly the same):

def recursfac(x,carryover=1):
    print 'x:',x,'carryover:', carryover
    if x > 1:
        carryover *= x
        result = recursfac(x-1, carryover)
    else:
        # done with recursion, start our way up
        result = carryover
    return result


_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to