# n! done recursively # ECS 10, May 26, 2009 # Matt Bishop # function to compute n! # parameter: n, a non-negative integer # returns: n! def fact(n): # base case: n = 0 if n == 0: return 1 # recursion here else: return n * fact(n-1) # wrapper for n!; check inputs # parameter: n, a non-negative integer # returns: n! on success, -1 on error # calls: fact(n): computes n! def fullfact(n): # quick check for numeric type try: n += 0 except TypeError: return -1 # check to be sure it's non-negative if n < 0: return -1 # now check that it is an int if n != int(n): return -1 # it's okay return fact(n)