# Fibonacci numbers done recursively # ECS 10, May 26, 2009 # Matt Bishop # function to compute fib(n) # parameter: n, a non-negative integer # returns: n-th Fibonacci number def fib(n): # base case: n = 0 if n == 0 or n == 1: return 1 # recursion here else: return fib(n-1) + fib(n-2) # wrapper for fib(n); check inputs # parameter: n, a non-negative integer # returns: nth Fibonacci number on success, -1 on error # calls: fib(n): computes nth Fibonacci number def fullfib(n): # quick check for numeric type try: n += 0 except TypeError: return -1 # check to be sure it's non-negative if n < 0: return -1 # now check that it is an int if n != int(n): return -1 # it's okay return fib(n)