# File: nfact.py # Program to compute n! # # Matt Bishop, MHI 289I Fall 2022 # # 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(input("Factorial: ")) except EOFError: pass 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("{0:d}! =".format(n), fact(n))