I got my definition wrong the code should be more like this def fib(n): if n==0: return elif n==1: return 1 elif n==2: return 1 else: return fib(n-2) + fib(n-1) thanks,rakesh > Date: Fri, 7 Feb 2014 21:40:49 -0800 > Subject: Re: [Tutor] learning recursion > From: denis.heidtm...@gmail.com > To: rakeshsharm...@hotmail.com > CC: da...@davea.name; tutor@python.org > > On Fri, Feb 7, 2014 at 9:05 PM, rakesh sharma > <rakeshsharm...@hotmail.com> wrote: > > Hi > > > > Shouldn't your code be like this > > > > def fib(n): > > if n==0: > > return 0 > > else: > > return n + fib(n-1) > > > > this works > > > >>>> for i in range(4): > > print fib(i) > > > > 0 > > 1 > > 3 > > 6 > >>>> > > interesting, but the Fibonacci sequence is 1,1,2,3,5,8,13,... > > -Denis H.
_______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor