Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #87832 > unrolled thread

fibonacci series what Iam is missing ?

Started byGanesh Pal <ganesh1pal@gmail.com>
First post2015-03-23 21:31 +0530
Last post2015-03-24 18:31 +0530
Articles 3 — 2 participants

Back to article view | Back to comp.lang.python


Contents

  fibonacci series what Iam is missing ? Ganesh Pal <ganesh1pal@gmail.com> - 2015-03-23 21:31 +0530
    Re: fibonacci series what Iam is missing ? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-03-24 11:11 +1100
      Re: fibonacci series what Iam is missing ? Ganesh Pal <ganesh1pal@gmail.com> - 2015-03-24 18:31 +0530

#87832 — fibonacci series what Iam is missing ?

FromGanesh Pal <ganesh1pal@gmail.com>
Date2015-03-23 21:31 +0530
Subjectfibonacci series what Iam is missing ?
Message-ID<mailman.70.1427126512.10327.python-list@python.org>
Hello team ,


[root@localhost Python]# cat  fibonacci-Sequence-3.py

## Example 2: Using recursion
def fib(n):
    if n == 0:
        return 0
    elif n == 1:
        return 1
    else:
        return fib(n-1) + fib(n-2)
print fib(5)

# python  fibonacci-Sequence-3.py
5

what Iam I missing in the program , I was expecting 0,1,1,2,3 ?

[toc] | [next] | [standalone]


#87853

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2015-03-24 11:11 +1100
Message-ID<5510abc9$0$13011$c3e8da3$5496439d@news.astraweb.com>
In reply to#87832
On Tue, 24 Mar 2015 03:01 am, Ganesh Pal wrote:

> Hello team ,
> 
> 
> [root@localhost Python]# cat  fibonacci-Sequence-3.py
> 
> ## Example 2: Using recursion
> def fib(n):
>     if n == 0:
>         return 0
>     elif n == 1:
>         return 1
>     else:
>         return fib(n-1) + fib(n-2)
> print fib(5)
> 
> # python  fibonacci-Sequence-3.py
> 5
> 
> what Iam I missing in the program , I was expecting 0,1,1,2,3 ?


Python does not automatically print all return statements. If you want it to
print the intermediate values produced, you will need to add print before
each return:


py> def fib(n):
...     if n == 0:
...         result = 0
...     elif n == 1:
...         result = 1
...     else:
...         result = fib(n-1) + fib(n-2)
...     print result,  # trailing comma means no newline
...     return result
...
py> fib(3)
1 0 1 1 2
2
py> fib(5)
1 0 1 1 2 1 0 1 3 1 0 1 1 2 5
5


If you want to print a list of Fibonnaci values, you need to call the
function in a loop. Removing the "print result" line again, you can do
this:

py> for i in range(6):
...     print fib(i),
...
0 1 1 2 3 5




-- 
Steven

[toc] | [prev] | [next] | [standalone]


#87883

FromGanesh Pal <ganesh1pal@gmail.com>
Date2015-03-24 18:31 +0530
Message-ID<mailman.101.1427202069.10327.python-list@python.org>
In reply to#87853
On Tue, Mar 24, 2015 at 5:41 AM, Steven D'Aprano
<steve+comp.lang.python@pearwood.info> wrote:

> Python does not automatically print all return statements. If you want it to
> print the intermediate values produced, you will need to add print before
> each return:
>
>
> py> def fib(n):
> ...     if n == 0:
> ...         result = 0
> ...     elif n == 1:
> ...         result = 1
> ...     else:
> ...         result = fib(n-1) + fib(n-2)
> ...     print result,  # trailing comma means no newline
> ...     return result
> ...
> py> fib(3)
> 1 0 1 1 2
> 2
> py> fib(5)
> 1 0 1 1 2 1 0 1 3 1 0 1 1 2 5
> 5
>
>
> If you want to print a list of Fibonnaci values, you need to call the
> function in a loop. Removing the "print result" line again, you can do
> this:
>
> py> for i in range(6):
> ...     print fib(i),
> ...
> 0 1 1 2 3 5


Thanks you Steven and others ( Dave, Chris and Terry ) , for having
such good discussion on this topic and benefiting me in more than one
way's. Thank you

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web