# Test file # Matt Bishop, February 27, 2012 # ECS 10, Winter Quarter 2012 # function to compute n! # parameter: n, must be non-negative integer # returns: n! on success # -1 on failure (bad parameter) def fact(n): # check for error in call try: if n < 0 or n != int(n): return -1 except: return -1 # now compute n! by repeated multiplication fact = 1 for i in range(1, n+1): fact *= i # return it return fact # Main routine to enable us to test fact() def main(): # get the input n = int(input("[[WARNING: numbers only! Negative to quit]] n: ")) # loop until quit while n >= 0: # say what you got so user can check print("%d! = %d" % (n, fact(n))) # get another input n = int(input("[[WARNING: numbers only! Negative to quit]] n: ")) # all done! say what user typed print("Quitting: n =", n) main()