# File: nfact.py # Program to compute n! # # Matt Bishop, MHI 289I, Winter 2018 # # compute n! # assumes n is a non-negative integer # 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 # # input number try: n = int(raw_input("Factorial: ")) except: print "Need an integer" else: # it better be non-negative! if n < 0: print "Need a non-negative integer" else: # compute n! and announce it print "%d! =" % n, fact(n)