# File: nfact.py # Program to compute n! # # Matt Bishop, ECS 10, Fall 2012 # # compute n! # def fact(n): # base case: 0! = 1 (by definition) if n == 0: return 1 # recursion: n! = n * (n-1)! return n * fact(n-1) # # the main routine # def main(): # input number try: n = int(input("Factorial: ")) except: print("Need an integer") return # it better be non-negative! if n < 0: print("Need a non-negative integer") return # compute n! and announce it print("%d! =" % n, fact(n)) main()