#Using recursion to get fibonacci numbers #Our getFib function, needs a whole number def getFib(number): #assert the number is whole assert(isinstance(number,int)),"Needs a whole number" #Base case. When the number is 1 #we know what we want to return #so there is no need for more recursion #we can just return our value if number == 1 or number ==0: return 1 #For any number apart from 1 #we do another recursion of the program #this will happen until number == 1 else: return getFib(number-1)+getFib(number-2) print("10th fibonacci number: %i" %getFib(10)) print("20th fibonacci number: %i" %getFib(20))
Edited by sneekula: n/a
Are you able to help answer this sponsored question?
Questions asked by members who have earned a lot of community kudos are featured in order to give back and encourage quality replies.