[Tutor] learning recursion
rakesh sharma
rakeshsharma14 at hotmail.com
Sat Feb 8 11:06:20 CET 2014
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.heidtmann at gmail.com
> To: rakeshsharma14 at hotmail.com
> CC: davea at davea.name; tutor at python.org
>
> On Fri, Feb 7, 2014 at 9:05 PM, rakesh sharma
> <rakeshsharma14 at 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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20140208/a723eb8b/attachment.html>
More information about the Tutor
mailing list