'''lecture_34_factorial.py The factorial function computed using recursion Oliver W. Layton CS 151: Computational Thinking: Visual Media Spring 2021 ''' def factorial(n): '''n is an int >=0 that we are taking the factorial of e.g. factorial(4) = 4*factorial(3) ''' if n == 0: # Base case: where the recursion stops / where # we stop calling the function from within the function. # always needed when using recursion (otherwise you will # keep calling function from within the function # infinitely many times!) ans = 1 else: ans = n*factorial(n-1) return ans if __name__ == '__main__': answer = factorial(3) # factorial(3) = 3*2*1 = 6 # answer = factorial(4) # factorial(4) = 4*3*2*1 = 24 # answer = factorial(12) print(answer)