fibonacci series what Iam is missing ?

Ganesh Pal ganesh1pal at gmail.com
Tue Mar 24 09:01:06 EDT 2015


On Tue, Mar 24, 2015 at 5:41 AM, Steven D'Aprano
<steve+comp.lang.python at 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



More information about the Python-list mailing list